1

I saw already a question with very large number of decimal digits R rounding explanation.

round(62.495, digits=2)

gives me 62.49. I would expect already 62.5, but it seems, R (3.4.3, 3.5.0) rounds up only starting at 6, e.g.,

round(62.485, 2) == 62.48
round(62.486, 2) == 62.49.

For other reasons, I am using the option

options(digits.secs=6)

From what I have learnt, one rounds up starting at 5. I tested also with Python and Matlab. Matlab rounds up, Python 3.5.4 down.

How can I change the behaviour or is this definition different, e.g. between Europe and US?

1 Answers1

4

This is a floating point representation issue, 62.495 is actually represented by a slightly smaller number which then gets rounded downwards.

print(62.495,digits=22)
[1] 62.49499999999999744205

R's rounding is statistical rounding, or round half to even. It should round halves up or down to an even number, eg

round(0.5) # rounds the half down to 0
[1] 0
round(1.5) # rounds the half up to 2
[1] 2
James
  • 65,548
  • 14
  • 155
  • 193