Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r keeping only specific digits after the decimal point and printing the number

Tags:

r

In R, How can I ensure that i only print 3 characters/numbers after the decimal point? I thought that format command with nsmall parameter is sufficient but i am not getting required answer

> format(0.6791787, nsmall=3)
[1] "0.6791787"

I want 0.679

like image 246
user2543622 Avatar asked Sep 14 '25 05:09

user2543622


1 Answers

Using sprintf it would be

sprintf("%.3f", 0.6791787)
# [1] "0.679"

The number after the . and before the f is the number of digits to print after the decimal.

like image 92
Rorschach Avatar answered Sep 16 '25 23:09

Rorschach