Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
up vote 18 down vote accepted

Just specify the nearest multiple in the second argument.

Round[123.456, 0.01]


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

share|improve this answer
+1 for a cool trick! – R Hall Jul 4 '12 at 14:00
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]
share|improve this answer
(+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

Your Answer


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.