[R] Formatting numbers with a limited amount of digits consistently

Duncan Murdoch murdoch at stats.uwo.ca
Tue May 31 17:11:55 CEST 2005


Marc Schwartz wrote:

> Final note to Henrik: Note that the IEEE 754 rounding standard as
> implemented in R results in:
> 
> 
>>round(18.15, 1)
> 
> [1] 18.1
> 
>>formatC(18.15, format = "f", digits = 1)
> 
> [1] "18.1"
> 
>>sprintf("%5.1f", 18.15)
> 
> [1] " 18.1"
> 
> This is because the rounding method implemented is the "go to the even
> digit" approach. Thus, you don't get 18.2. 
> 
> See ?round for more information.

I don't think "go to the even digit" is being applied here:  ".1" is not 
  an even digit.

I suspect what's going on in this example is that 18.15 is not being 
represented exactly; it's stored internally as something slightly less 
than that value, so it rounds down.

You'd see the "go to the even digit" rule applied when rounding 17.5 or 
18.5, which can be represented exactly, being fractions with a power of 
2 in the denominator:

 > round(18.5, 0)
[1] 18
 > round(17.5, 0)
[1] 18

(This is very gratifying.  Usually when I try to predict the exact 
behaviour of round() or signif() I end up having to rewrite my 
prediction afterwards.  But this time I got it right. Honest!)

Duncan Murdoch




More information about the R-help mailing list