8

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
0

1 Answer 1

12

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
2
  • 1
    That's a nice one. Strangely haven't heard of it. Commented Apr 16, 2015 at 12:21
  • 3
    @DavidArenburg yep, the base package is full of wonders :-) . Even after 10 yrs I still find new tools in there. Commented Apr 16, 2015 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.