Is there a simple way of rounding a value either down to or to the nearest (1, 2, or 5) x 10^n where n is an integer? As in one of {…, .05 .1, .2, .5, 1, 2, 5, 10, 20, 50, 100…}
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can take the
d=floor(log10(n))of your numbernto get the scale, then divide by 10 to thedto normalize the number to the range of[1.0,10.0). From that point it should be easy to round because there are very limited possibilities. Once you’ve done that, multiply by 10 to thedto restore the number to the original range.The following function is in C, I don’t know enough about Objective-C to know if this is idiomatic.