Reputation: 181
I just trying to round in R number like:
> round(1.327076e-09)
I would like it to result in
> 1.33e-09
but results in
> 0
which function can use?
Upvotes: 12
Views: 5947
Reputation: 70633
Try signif
:
> signif(1.326135235e-09, digits = 3)
[1] 1.33e-09
Upvotes: 20
Reputation: 179448
The function round
will do rounding and you can specify the number of decimals:
x <- 1.327076e-09
round(x, 11)
[1] 1.33e-09
Rising to the challenge set by @Joris and @GavinSimpson - to use trunc
on this problem, do the following:
library(plyr)
round_any(x, 1e-11, floor)
[1] 1.32e-09
Upvotes: 2
Reputation: 66844
Use signif
:
x <- 1.327076e-09
signif(x,3)
[1] 1.33e-09
or sprintf
:
sprintf("%.2e",x)
[1] "1.33e-09"
Upvotes: 8