# Dynamically show a specific number of the fractional part's digits

What is the fastest way to cut off all n z digits of the fractional part of a real number?

(For example 12390.20934230)

I came up with this so far:

Round[l_List, z_] :=
IntegerPart[l] +
Module[{r = RealDigits[l]},
Fold[10 #1 + #2 &, 0, Take[r[[1]], {Max[r[[2]],0]+z, r[[2]]+z}]] 10^-z]


Best would be a fast pure function to run over an array, and optimal would be to just show the output rounded (aka ScientificForm), but let the actual numbers as they are.

-
Perhaps map ScientificForm over your array at the -1 level? – m_goldberg Mar 15 '13 at 18:49
First of all, it does not round the number the way I want it to, second I think after ScientificForm it is no number any more... – Buembel Mar 15 '13 at 18:53
What I suggested does not modify the contents of an array unless you assign the results back to a symbol bound to the array, which I did not suggest. Perhaps mapping NumberForm would be more to your liking. – m_goldberg Mar 15 '13 at 19:00
a) would you like the output to be assignable? that is, do you want to display the number in truncated form, or actually truncate? b) what do you mean "dynamically"? as the question is states, it has nothing to do with Dynamic or Manipulate. c) you want to run it over an array of what, and to do what? you just need a round function to map over a list of reals? or what? – acl Mar 15 '13 at 19:05
Round[x,10^-z] with integer z rounds to z digits. Would that do what you want? – Sjoerd C. de Vries Mar 17 '13 at 8:12

## 1 Answer

It's hard to figure out from your question what you want.
You probably can do it with NumberForm and its options, so here is an example:

nums = 5000^RandomReal[2, 10];      (* make some numbers *)

nums // InputForm                   (* look at the long form *)

{39753.38655208366, 372163.42335132253, 603781.8781407636, 312.9474417510306,
84106.95799695785, 8609.944109613456, 1.2586190926109136*^6,
25203.109639366387, 413952.63700106385, 8.649027381565293}


Define a formatting function using NumberForm and apply it:

myForm =
NumberForm[#, {6, 3}, ExponentFunction -> (If[# < 6, Null, 3 Quotient[#, 3]] &)] &;

formatted = nums // myForm          (* format the numbers *)


First @ formatted // InputForm      (* prove that the numbers are unchanged *)

{39753.38655208366, 372163.42335132253, 603781.8781407636, 312.9474417510306,
84106.95799695785, 8609.944109613456, 1.2586190926109136*^6,
25203.109639366387, 413952.63700106385, 8.649027381565293}

-
Thank you, next time I will be more precise I promise. – Buembel Mar 19 '13 at 1:25