# How to format numbers with at least one figure after the decimal point

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.

Here is an idea:

SetAttributes[myForm, Listable]
myForm[x_] :=
If[Abs[Mod[x, 1]] <= $MachineEpsilon, NumberForm[x, {Infinity, 1}], x]  • This is a pretty good way for "normal" numbers. But how to to handle ScientificForm[300.]? Commented Apr 25, 2012 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 Commented Apr 25, 2012 at 10:55 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}  • you were a few seconds faster ;-) – user21 Commented Apr 25, 2012 at 9:53 • If only a few seconds faster, you were fast too ! Commented Apr 25, 2012 at 9:58 • 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. Commented Apr 25, 2012 at 10:16 • @PeterBreitfeld I updated my answer to satisfy your needs. Commented Apr 25, 2012 at 11:45 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:

• 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`. Commented Apr 25, 2012 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. Commented Apr 25, 2012 at 19:36