jeromyanglim / learning_r

assorted notes to self while learning R
1 stars 0 forks source link

How to round a number so that it always shows a specified number of digits after the decimal in R? #2

Closed jeromyanglim closed 12 years ago

jeromyanglim commented 12 years ago

As per this post the following will force two digits

format(round(x, 2), nsmall = 2)

And here's a function that takes a numeric variable and a specified number of digits

force_digits <- function(x, digits=2) format(round(x, digits), nsmall = digits)