stefan
stefan

Reputation: 181

R round exponential number

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

Answers (3)

Roman Luštrik
Roman Luštrik

Reputation: 70633

Try signif:

enter image description here

> signif(1.326135235e-09, digits = 3)
[1] 1.33e-09

Upvotes: 20

Andrie
Andrie

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

James
James

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

Related Questions