Rounding problem R vs Excel
On Mon, 2 Jun 2003 20:50:20 -0400, you wrote:
Does this script do what you want?
cround <- function(x,digits=0) {
a <- ifelse(x>0,.5,-.5)
if (digits==0) {
floor(x+a)
} else {
m <- 10^digits
floor(x*m+a)/m
}
}
No, the problem is that R uses binary formats, and some numbers aren't representable there. So for example,
cround(0.145,2)
[1] 0.14 because 0.145 isn't representable exactly, and is actually being represented as 0.14999999999 or something similar. Duncan Murdoch