2
$\begingroup$

I want to pad with ending zero and tried the following code

d = 7;
f[x_] := 150/x
g[x_] := 2 Log[x] - 3
Table[{x, StringPadRight[ToString[NumberForm[N[f[x]], d]], d, "0"], 
    StringPadRight[ToString[NumberForm[N[g[x]], d]], d, "0"]}, {x, 20,
     50}] // N // TableForm

which gave

 20. 7.50000 2.99146
 21. 7.14285 3.08904
 22. 6.81818 3.18208
 23. 6.52173 3.27098
 24. 6.25000 3.35610
 25. 6.00000 3.43775

which is kind of 'OK'.

Are there any better/shorter code to get this result?

Also, is there a simple way to remove the period for the integers in the first column?

$\endgroup$
3
  • 3
    $\begingroup$ Maybe define: numb[n_] := NumberForm[N[n, d], d] then do Table[{x, numb@f[x], numb@g[x]}, {x, 20, 50}] // TableForm $\endgroup$
    – flinty
    Nov 19 '20 at 13:04
  • $\begingroup$ That worked well, thank you. Perhaps make it an answer? $\endgroup$
    – mf67
    Nov 19 '20 at 15:45
  • 3
    $\begingroup$ This might be better: DecimalForm[Table[{x, N[f[x]], N[g[x]]}, {x, 20, 50}], {d, d - 2}]. Among other things it will get the rounding correct. $\endgroup$ Nov 19 '20 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.