0

i have an issue when i using this function in R. I don't know why trunc(z) is 637 instead of 638 as x = 51/80*100.

x <- 51/80*100
x
#[1] 63.75

z = abs(x)*10^1
z = z + 0.5
z
#[1] 638
z = trunc(z)
z
#[1] 637
z = z/10^1
z
#[1] 63.7
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

0

As @d.b already pointed out that z is numeric and with numerics you don't necessarily have what you see.

Check

formatC(z, digits = 14, format = "f")
#[1] "637.99999999999989"

(You might have to change digits argument on your system)

Also if you convert it to integer, you get

as.integer(z)
#[1] 637

You can round the numeric before using them

trunc(round(z))
#[1] 638
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213