round number to the first 3 digits (start with digit != 0)
Asked Answered
P

1

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
Pricillaprick answered 16/4, 2015 at 12:11 Comment(0)
P
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
Parasympathetic answered 16/4, 2015 at 12:20 Comment(2)
That's a nice one. Strangely haven't heard of it.Noreennorene
@DavidArenburg yep, the base package is full of wonders :-) . Even after 10 yrs I still find new tools in there.Cincinnatus

© 2022 - 2024 — McMap. All rights reserved.