round number to the first 3 digits (start with dig

2020-04-03 10:02发布

问题:

is there a predefined format-function that rounds a number to the first 3 digits? (The start should be a numbers != 0)

-0.02528498    to -0.0253
 1.857403      to 1.86     
 2060943       to 2060000
 0.00006513832 to 0.0000651

回答1:

You can use the function signif:

signif(-0.02528498, 3)
# [1] -0.0253
signif(1.857403, 3)
# [1] 1.86
signif(2060943, 3)
# [1] 2060000
signif(0.00006513832, 3)
# [1] 0.0000651