Other answers have provided solutions but they have not directly addressed why this happens. The use of N
on exact values with a numeric second parameter (e.g. 10
) produces arbitrary-precision output, and these values are by default displayed in full without truncation.
Compare:
5.000000 (* machine precision Real *)
5`7 (* arbitrary precision *)
5.
5.000000
The methods in Alan's and Michael's answers get around this by converting to machine precision and then adjusting output. However if you want to print more than $MachinePrecision
digits these will not work. Carl's answer comes closer but it also has a problem in that more digits than requested may be printed.
If you specifically want to suppress trailing zeros then you could process to affect that.
For whole numbers simply:
rep = r_Real /; r == Round[r] :> N @ Round[r];
N[Table[4^(i/4), {i, 1, 4}], 25] /. rep
{
1.414213562373095048801689,
2.,
2.828427124746190097603377,
4.
}
If you need to preserve the arbitrary precision nature of the output when used as input use Interpretation
:
rep2 = r_Real /; r == Round[r] :> Interpretation[N @ Round[r], r];
If you want to trim trailing zeros that are not immediately following the decimal point you might turn to string processing:
rep3 = r_Real :>
Interpretation[
RawBoxes @ StringReplace[ToString[r],
Shortest[x__] ~~ "0" .. ~~ EndOfString :> x], r];
N[Table[4^(i/4), {i, 1, 4}]/10, 25] /. rep3 // Column
0.1414213562373095048801689
0.2
0.2828427124746190097603377
0.4
If you want to do this globally for a session consider $PrePrint
, e.g.
$PrePrint = # /. rep3 &;
Then you can simply write:
N[Table[4^(i/4), {i, 1, 4}]/10, 10]