I need to convert a dataframe of floats with 2-decimal point precision to strings. However, as.character drops zeros in the decimal part:
> as.character(1.00)
We can use sprintf
sprintf
sprintf("%0.2f", 1.10) #[1] "1.10" sprintf("%0.2f", 1.00) #[1] "1.00"
We can also use the format function with the nsmall argument to be 2.
nsmall
format(1, nsmall = 2) # [1] "1.00" format(1.1, nsmall = 2) # [1] "1.10"