How to round any decimal number less than one to show only one number at the end?.
0.00045454 -> 0.0005
0.024 -> 0.02
0.2 - > 0.2
0.000000007020 - > 0.000000007
0.000000008 - > 0.000000008
CodePudding user response:
Use log10
with ceiling
.
fRoundEnd <- function(x) round(x, ceiling(-log10(x)))
fRoundEnd(0.00045454)
#> [1] 5e-04
fRoundEnd(0.024)
#> [1] 0.02
fRoundEnd(0.2)
#> [1] 0.2
fRoundEnd(0.000000007020)
#> [1] 7e-09
fRoundEnd(0.000000008)
#> [1] 8e-09
fRoundEnd(2022)
#> [1] 2000
fRoundEnd(c(0.00045454, 0.024, 0.2, 0.000000007020, 0.000000008, 2022))
#> [1] 5e-04 2e-02 2e-01 7e-09 8e-09 2e 03
CodePudding user response:
You can use signif
signif(c(0.00045454, 0.024, 0.2, 0.000000007020, 0.000000008, 2022), 1)
# [1] 5e-04 2e-02 2e-01 7e-09 8e-09 2e 03