6
$\begingroup$

Mathematica has several number formats such as ScientificForm, EngineeringForm, etc. Are there any that can be configured to omit a redundant decimal point, or alternatively: work on exact numbers?

I want either

myForm[2.*10^10]

$2\times 10^{10}$

or

myForm[2*10^10]

$2\times 10^{10}$

(Of course for 2.5*10^10 the decimal point would be required.)

With ScientificForm, I get

ScientificForm[2.*10^10]

$2.\times 10^{10}$

ScientificForm[2*10^10]

$20000000000$

I can piece the result together with strings or HoldForm, but my question is: is there a builtin solution?


Why do I think there may be a builtin way? Because of the tick marks here:

Graphics[{Circle[{0, 0}, 10^10]}, Frame -> True]

enter image description here

It is however possible that this formatting is done by the front end and isn't available in the kernel.

What is this needed for? Precisely for tick labels. I know that a reasonable workaround for this application is using $2.0\times 10^{10}$, and I have been using that in the past. This time I want simply $2$ instead of $2.0$ or $2.$.

$\endgroup$
10
$\begingroup$

You may use the NumberFormat option of ScientificForm.

With

nf = With[{m = ToExpression@#1},
   Row[{If[PossibleZeroQ[m - Round[m]], ToString@Round[m], #1],
     Sequence @@ If[#3 == "", {}, {"×", #2^#3}]}]] &

The arguments to NumberFormat are strings so you have to convert to test.

Then

v = {2.*10^10, 2.000001, 6885.45559^2};
ScientificForm[v, NumberFormat -> nf]

enter image description here

Hope this helps.

Update

Using StringTrim from comments and also added spacing.

nf = Row[{StringTrim[#1, "."], 
    Sequence @@ If[#3 == "", {}, {Spacer[3], "×", Spacer[3], #2^#3}]}] &

ScientificForm[v, NumberFormat -> nf]

Mathematica graphics

| improve this answer | |
$\endgroup$
  • $\begingroup$ Could we simplify this to simply Row[{StringTrim[#1, "."], "\[Times]", Superscript[#2, #3]}] &? A problem both with this and with your solution is that the spacing around the operator $\times$ isn't correct. $\endgroup$ – Szabolcs Sep 28 '16 at 11:42
  • $\begingroup$ @Szabolcs That does not work for numbers like 2.0001. You end up with 2x10. $\endgroup$ – Edmund Sep 28 '16 at 12:33
  • $\begingroup$ @Szabolcs See update for spacing and using StringTrim. $\endgroup$ – Edmund Sep 28 '16 at 12:41

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.