I'm interested to know why as.character(5.0)
returns 5
but as.character(5.1)
returns 5.1
in R. I tried to get an answer by reading the documentation but had no luck.
I'm interested to know why as.character(5.0) returns 5
The key word here is "returns." What do you mean by that? Note that typing this in the console gives you 5:
> 5.0
[1] 5
5 is the same things as 5.0 for the purposes of calculation. So what you probably really care about is how 5 is printed. You thus need to use joran's method or a function like sprintf
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With