# How do you round numbers so that it affects computation?

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.

• Round[x,a] rounds to the nearest multiple of a – acl Jul 4 '12 at 12:46
• @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

## 4 Answers

Just specify the nearest multiple in the second argument.

Round[123.456, 0.01]


123.46

• 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
• There are some problems with Round. For example, try Round[4.811] and you get 4.8100000000000005, not 4.81 as expected. – Murta Nov 9 '14 at 22:56
• Nice observation. That also occurs with 481/100. but not with N[481/100] so the safer usage is to round to integer like N[Round[100*4.811]/100] – Chris Degnen Nov 9 '14 at 23:19
• @ChrisDegnen I'm using N@Round[100*4.811,1/100] to solve that. But take care with automatic Map compilation, details here: 65298 – Murta Nov 10 '14 at 13:21

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

• 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
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

• (+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

Another solution is

Round1[x_, n_] := With[{m = Round[Log10[Abs[x]]]}, Round[x 10^(n - m)] 10.^(m - n)];

(*m estimates the scale of x, n sets the number precision, Abs function enables negative number.*)

Round1[-3.46473*10^-15, 4] // InputForm
(*-3.465*^-15*)