Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
A related post is: Rationalizing Numerical Output –  Jens Apr 25 '12 at 14:56
add comment

3 Answers

up vote 7 down vote accepted

Here is an idea:

SetAttributes[myForm, Listable]
myForm[x_] := 
 If[Abs[Mod[x, 1]] <= $MachineEpsilon, NumberForm[x, {Infinity, 1}], 
  x]
share|improve this answer
    
This is a pretty good way for "normal" numbers. But how to to handle `ScientificForm[300.]? –  Peter Breitfeld Apr 25 '12 at 10:39
    
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. –  user21 Apr 25 '12 at 10:55
add comment

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

share|improve this answer
2  
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. –  murray Apr 25 '12 at 16:17
    
@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. –  Peter Breitfeld Apr 25 '12 at 19:36
add comment

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}
share|improve this answer
1  
you were a few seconds faster ;-) –  user21 Apr 25 '12 at 9:53
2  
If only a few seconds faster, you were fast too ! –  Artes Apr 25 '12 at 9:58
2  
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. –  Peter Breitfeld Apr 25 '12 at 10:16
    
@PeterBreitfeld I updated my answer to satisfy your needs. –  Artes Apr 25 '12 at 11:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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