I have a number equal to 0.5, I would like to save two digits to make the number 0.50. So far, the last digit is zero, so it cannot always appear.
I used round (0,5,2), but it does not work
You can cheat using:
y <- 0.5 formatC(round(y,2),2,format="f")
Note that this changes to a character. Therefore, this is for display only.
Another option is to use sprintf
sprintf
y <- 0.5 sprintf("%0.2f", round(y, 2)) [1] "0.50"
EDITOR: According to Wojciech Sobale (below)
sprintf("%0.2f", y)
should be enough.
sprintf("%0.2f", 0.478) [1] "0.48"