# How to display a different number of significant digits in each column of TableForm output?

Say I create a table with three columns of numbers. Is there a way to control the number of significant digits displayed in each column. To make this concrete, take the following command:

Table[{m, Sum[1/2.^n, {n, 1, m}], Sum[1./n, {n, 1, m}]}, {m, 1, 20.}] // TableForm


This produces the following table:

1   0.5         1.
2   0.75        1.5
3   0.875       1.83333
...
18  0.999996    3.49511
19  0.999998    3.54774
20  0.999999    3.59774


The first column is fine as whole numbers, but I want the second column to have more decimal places, say, eight places, and the third column to have, say, four places. I tried putting one of the Sum[] functions inside the N[] function, but this had absolutely no effect. Is there another way to individually format each column of output?

-

Yes, just wrap the Sum functions in NumberForm (documentation) like this:

Table[{m, NumberForm[Sum[1/2.^n, {n, 1, m}], 8],
NumberForm[Sum[1./n, {n, 1, m}], 4]}, {m, 1, 20.}] // TableForm


Output:

    1   0.5         1.
2   0.75        1.5
3   0.875       1.833
4   0.9375      2.083
5   0.96875     2.283
6   0.984375    2.45
7   0.9921875   2.593
8   0.99609375  2.718
9   0.99804688  2.829
10  0.99902344  2.929
11  0.99951172  3.02
12  0.99975586  3.103
13  0.99987793  3.18
14  0.99993896  3.252
15  0.99996948  3.318
16  0.99998474  3.381
17  0.99999237  3.44
18  0.99999619  3.495
19  0.99999809  3.548
20  0.99999905  3.598


If you really wanted the trailing zeros after the decimal place, then PaddedForm is what you need. The second argument shows the total number of digits and the number of digits after the decimal place that are to be shown.

    Table[{m, NumberForm[Sum[1/2.^n, {n, 1, m}], {9, 8}],
PaddedForm[Sum[1./n, {n, 1, m}], {5, 4}]}, {m, 1, 20.}] // TableForm


Output:

1   0.50000000   1.0000
2   0.75000000   1.5000
3   0.87500000   1.8333
4   0.93750000   2.0833
5   0.96875000   2.2833
6   0.98437500   2.4500
7   0.99218750   2.5929
8   0.99609375   2.7179
9   0.99804688   2.8290
10  0.99902344   2.9290
11  0.99951172   3.0199
12  0.99975586   3.1032
13  0.99987793   3.1801
14  0.99993896   3.2516
15  0.99996948   3.3182
16  0.99998474   3.3807
17  0.99999237   3.4396
18  0.99999619   3.4951
19  0.99999809   3.5477
20  0.99999905   3.5977

-

The N function has no effect because you already use approximate numbers, namely machine numbers (by using 2. instead of 2 resp. 1. instead of 1). For example

N[Sqrt[2],30]
(*
==> 1.41421356237309504880168872421
*)
N[Sqrt[2.],30]
(*
==> 1.41421
*)


So the safest way to get what you want is to replace the approximate numbers by exact numbers in your code.

You can also explicitly give an approximate number with sufficient number of digits, e.g.

Sqrt[2.30]
(*
==> 1.414213562373095048801688724210
*)


Another way to get more digits in the output, as Verbeia has noted, is to use NumberForm or PaddedForm. However be aware that this only changes the display of the numbers, not their actual accuracy, e.g.

NumberForm[Sqrt[2.],30]
(*
==> 1.414213562373095
*)

Up to 16 digits, machine numbers are (normally) OK, but if you ever need more, then you have to use one of the other methods. If you can afford it, the safest is always to calculate using exact numbers and only in the last step apply N.
I can imagine the use of 1. instead of 1 in your formula is a habit derived from programming languages like Fortran, C or Java where 1/2 is interpreted as integer division and therefore gives 0. However in Mathematica this is not the case; here 1/2` is one half, just like in mathematics.