'keeping trailing zeros in prettyNum()

I need to print my numbers with the same number of trailing zeros (automatically "guessed" for each input), and can't find the right parameter in prettyNum(). What I want is the following:

prettyNum(c(1, 1.1, 2), ...)
# "1.0" "1.1" "2.0"

prettyNum(c(100, 1.1, 2, 2.25), ...)
# "100.00" "  1.10" "  2.00" "  2.25"
r


Solution 1:[1]

format() can easily achieve what you want.

format(c(1, 1.1, 2))
# [1] "1.0" "1.1" "2.0"

format(c(100, 1.1, 2, 2.25))
# [1] "100.00" "  1.10" "  2.00" "  2.25"

If you don't need the leading spaces, just add trim = TRUE.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Darren Tsai