Home > Net >  keeping trailing zeros in prettyNum()
keeping trailing zeros in prettyNum()

Time:04-21

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"

CodePudding user response:

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.

  •  Tags:  
  • r
  • Related