Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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
1  
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
add comment

1 Answer 1

up vote 1 down vote accepted

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 *)

Mathematica graphics

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}
share|improve this answer
    
Thank you, next time I will be more precise I promise. –  Buembel Mar 19 '13 at 1:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.