I have two pattern of numbers:
x1 = 0.0003577
x2 = 0.2365483
How can I round them to be like:
x1 = 0.00035
x2 = 0.23
so only two or one number after the last zero after comma appears?
I have two pattern of numbers:
x1 = 0.0003577
x2 = 0.2365483
How can I round them to be like:
x1 = 0.00035
x2 = 0.23
so only two or one number after the last zero after comma appears?
you can use signif in the base package. The function rounds the values in its first argument to the specified number of significant digits.
x1 = 0.0003577
x2 = 0.2365483
#where 2 is the number of significant digit
signif(x1,2)
[1] 0.00036
signif(x2,2)
[1] 0.24
You can define a user function f like below
f <- function(x) {
p <- 10^(-ceiling(log10(x)) + 2)
trunc(x * p) / p
}
and you will get
> f(x1)
[1] 0.00035
> f(x2)
[1] 0.23