0.5 is displayed as 0.5, even when I use the round (0.5.2)

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

+4
source share
2 answers

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.

+3
source

Another option is to use 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" 
+2
source

All Articles