0

I have been troubled by this simple question. I'd like to round numbers that has the last digit in 5 (like 0.45, or 0.85) to the format of 1 digit. When I used the round() function in R on, for example 0.45, it didn't round up to 0.5, but down to 0.4:

round(x = 0.45, digits = 1)

#> 0.4

I am aware that it may relate to the features of floating-point numbers and the "round half to even" rounding method of the round() function. I am just wondering how can I get the 'correct' rounding in R?

Grasshopper_NZ
  • 302
  • 1
  • 10
  • 1
    As counter-intuitive as it may be, it is well-documented: see [`?round`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html), it says: *"Note that for rounding off a 5, the IEC 60559 standard (see also ‘IEEE 754’) is expected to be used, ‘_go to the even digit_’."* This can be verified with `round(x = c(0.45, 0.55), digits = 1)` returning `c(0.4, 0.6)`. – r2evans Aug 13 '23 at 23:38
  • 1
    As to how to round in the way that I (for one) was taught in school (more than a few years ago ;-), see the dupe-link, where `round2(x = c(0.45, 0.55), digits = 1)` returns `c(0.5, 0.6)` (what one might prefer). – r2evans Aug 13 '23 at 23:40

0 Answers0