1
Convert[0.05263, Percent] /. Percent -> "%"

I would like to remove the trailing "%" but if I put Percent->"" it concatenates a literal empty pair of quotes.

Thanks.

Edit: I should have explained it works well in the Mathematica interactive evaluator but not in an InputField where it will concatenate the said quotes.

dabd
  • 839
  • 4
  • 13

2 Answers2

4

For the older "Units`" package units are just symbols which are multiplied to numbers. If you want to get rid of them, just replace them with 1:

 Convert[0.05263, Percent] /. Percent -> 1

In such cases, it is often very useful to use InputForm or even FullForm to explicitly check with what you are dealing and what is returned, e.g.:

 Convert[0.05263, Percent] /. Percent -> 1 // FullForm

which shows that now the number is all that's left, without any spurious relicts of the former units caculation...

Albert Retey
  • 23,585
  • 60
  • 104
0

You can the following to check the FullForm of your code (makes it easy to understand how Mathematica's internal structures works. Check Mathematica's reference if you want to know details about this.

FullForm[Convert[0.05263, Percent]]
(* Result: Times[5.263`,Percent] *)

There you see that it is a simple multiplication (Head: Times) of two elements. Then you can either use Albert Retey's solution or do a simple

First[Convert[0.05263, Percent]]
(* Result: 5.263 *)

This is especially usefull if you have a more complex output like

FullForm[SpeedOfLight]
(* Result: Times[299792458,Meter,Power[Second,-1]] *)

Then you can do a quick

First[SpeedOfLight]
(* Result: 299792458 *)

to get the value of this expression without the units.

dotcs
  • 587
  • 2
  • 10