10
$\begingroup$

I'm searching for a way to get Reals formatted with at least one figure after the decimal point. So e.g. 3. should be printed as 3.0 and ScientificForm[300,3] as $3.0\times 10^2$. I tried the options NumberFormat, PaddedForm, Padding etc. with no success.

$\endgroup$
1

3 Answers 3

9
$\begingroup$

Here is an idea:

SetAttributes[myForm, Listable]
myForm[x_] := 
 If[Abs[Mod[x, 1]] <= $MachineEpsilon, NumberForm[x, {Infinity, 1}], 
  x]
$\endgroup$
2
  • $\begingroup$ This is a pretty good way for "normal" numbers. But how to to handle `ScientificForm[300.]? $\endgroup$ Apr 25, 2012 at 10:39
  • $\begingroup$ I am not sure how this would work. ScientificForm is a predefined form and perhaps it makes more sense to write your own form and use that in place of of ScientificForm. $\endgroup$
    – user21
    Apr 25, 2012 at 10:55
10
$\begingroup$

Maybe you want this :

NumberForm[#, {4, 2}] &@{3., 3, 4.}
{3.00, 3.00, 4.00}

This should do what you need :

number[x_] := If[FractionalPart[x] == 0, NumberForm[x, {Infinity, 1}], x]

Let's test this function

number /@ {3., 3, -5.53, 4.356, 55}
{3.0, 3.0, -5.53, 4.356, 55.0}
$\endgroup$
4
  • 1
    $\begingroup$ you were a few seconds faster ;-) $\endgroup$
    – user21
    Apr 25, 2012 at 9:53
  • 2
    $\begingroup$ If only a few seconds faster, you were fast too ! $\endgroup$
    – Artes
    Apr 25, 2012 at 9:58
  • 2
    $\begingroup$ No, that's not exactly what I want. I want to change the output of all numbers which Mma displays ending with a dot to be displayed with one appended 0. So 4.1 is good, but 4. should print 4.0. I don't want to have a fixed number of decimals. $\endgroup$ Apr 25, 2012 at 10:16
  • $\begingroup$ @PeterBreitfeld I updated my answer to satisfy your needs. $\endgroup$
    – Artes
    Apr 25, 2012 at 11:45
3
$\begingroup$

With the help of @ruebenko and @Artes I came to the following solution, writing my own expForm as an alternative to ScientificForm:

ClearAll[dtZahl]
SetAttributes[dtZahl, Listable]
dtZahl[x_?NumberQ] :=

 If[Abs[Mod[x, 1]] <= $MachineEpsilon, NumberForm[x, {Infinity, 1}], x]
dtZahl[x_] := x

ClearAll[expForm]
expForm[x_?NumberQ] :=
 Module[{me = MantissaExponent[x], num, exp},
  If[MemberQ[{0, 0., 1, 1., -1, -1.}, x], Return[x]];
  exp = Superscript["\[CenterDot]10", me[[2]] - 1];
  num = NumberForm[N[me[[1]]]*10 // dtZahl, 3];
  If[me[[1]] == 0.1, (* no mantissa*)
   num = ""; 
   exp = Superscript[10, me[[2]] - 1],
   If[me[[2]] == 1,  (*range 0..10*)
     exp = ""], (*else*)
   ];
  Row[{num, exp}]
  ]
expForm[x_] := x

This looks now as I want:

expForm /@ {0, 1, -1, 1.23, 12.3, 123, -123, \[Pi], 0.1, 0.01, 0.0001, 0.123, 0.0123}

with the output:

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ I thought you always wanted a decimal point and always wanted at least one digit after the decimal point, even if it's 0. Your expForm[1] gives 1 but according to the original question should give 1.0. $\endgroup$
    – murray
    Apr 25, 2012 at 16:17
  • $\begingroup$ @murray Yes, but for 0 and 1 I find giving an Integer is fine. In german numbers are never written in the form "27." or ".27" etc, the dot (we use a comma) has alway to be between to digits. $\endgroup$ Apr 25, 2012 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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