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.


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.

  • $\begingroup$ Convert does not appear to be a built-in Mathematica function. Are you using a package where Convert is defined? $\endgroup$ – DavidC Apr 26 '13 at 12:25
  • $\begingroup$ Perhaps pre-9 Units package? $\endgroup$ – cormullion Apr 26 '13 at 12:33
  • $\begingroup$ Yes I am using Units. So my code has a Needs["Units`"]. I am using Mathematica 9. $\endgroup$ – dabd Apr 26 '13 at 12:36

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...

| improve this answer | |

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

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

Then you can do a quick

(* Result: 299792458 *)

to get the value of this expression without the units.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.