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.

I'm trying to make a demonstration of how rounding to different numbers of digits affects things but I can't find a way to round numbers to a specified number of digits.

The Roundfunction only round to the nearest whole integer, and that is not what I always want. Other ways seems to only change the way the numbers are displayed, not how they are internally stored.

I want to throw away precision, but it seems Mathematica doesn't want to allow me to do this. As an example: I would like to round 3.4647 to just 3.5 or 3.46.

There must be some way to do this, but I can't for the life of me find it.

share|improve this question
4  
Round[x,a] rounds to the nearest multiple of a –  acl Jul 4 '12 at 12:46
2  
@acl Actually, that's not quite true: when a is a machine value, then after the rounding, the result has to be expressed in double precision binary, causing further rounding in the 52nd digit (base 2). Normally nobody would care, but if you're studying the effects of rounding and your investigation takes you into the realm of tiny changes and high precision, this effect could become important. –  whuber Jul 4 '12 at 20:05
    
@whuber right, that was pasted from the docs. That is, I was pointing out that Round does what is being asked for, and this is mentioned at the top of the Round doc page. –  acl Jul 4 '12 at 20:12
add comment

3 Answers

up vote 13 down vote accepted

Just specify the nearest multiple in the second argument.

Round[123.456, 0.01]

123.46

share|improve this answer
    
Ah! Now I feel stupid. I did look at that but for some reason I assumed a had to be a whole number. –  Mr Alpha Jul 4 '12 at 23:17
add comment

Suppose Round did not take a second argument as it does. What to do?

myround[n_, a_] := Round[n/a] a

myround[π, 0.001]

myround[π, 1/7]
3.142

22/7
share|improve this answer
    
+1 for a cool trick! –  R Hall Jul 4 '12 at 14:00
1  
Now, suppose Round didn't exist. round[num_] := Sign[num] (1 + Floor[Abs[num] - 0.5]) –  Rojo Jul 4 '12 at 15:21
    
sign=Boole[# > 0] &, floor = IntegerPart[#] + (Sign[#] - 1)/2 &, integerPart = ToExpression@First@StringSplit[ToString@#, "."] & –  Rojo Jul 4 '12 at 15:26
    
Now, if not even Boole existed, I would consider going checking out Maple or Matlab –  Rojo Jul 4 '12 at 15:27
add comment
round1[x_, n_] := Ceiling[10^n x]/10^n // N
round2[x_, n_] := Floor[10^n x]/10^n // N
round1[3.4647, 1]
round2[3.4647, 2]
3.5
3.46
share|improve this answer
2  
(+1) This is a good approach--provided you omit the // N at the ends of the functions! Because the OP wants to study the effects of rounding, they should take advantage of MMA's exact arithmetic to do their calculations. This will demonstrate that the internal rounding implicit in the conversion to double-precision floats is not affecting anything. Also, to really round, rather than truncate, you should add 1/2, as in Ceiling[10^n x + 1/2]/10^n. –  whuber Jul 4 '12 at 20:07
    
@whuber Thanks for un upvote. I used //N to provide the number wanted by the OP, otherwise it would return a rational number. –  Artes Jul 5 '12 at 19:29
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.