jackjohnson44
asked on
how do I find the closest number divisible by 10
I need a function where it will give me the closest number divisible by 10. The input is a decimal so 85.92 is valid and the output would be 90.
so if the input was
81.9 = 80
84.34 = 80
89.11 = 90
91.4 = 90
85.0 = 90 (this wouldn't happen much because the input is a decimal)
so if the input was
81.9 = 80
84.34 = 80
89.11 = 90
91.4 = 90
85.0 = 90 (this wouldn't happen much because the input is a decimal)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this logic in your code
Divide by 10, then truncate, then multiply by 10.
229 / 10 = 22,90000...
Math.Truncate(22,9) = 22
22 * 10 = 220.
Divide by 10, then truncate, then multiply by 10.
229 / 10 = 22,90000...
Math.Truncate(22,9) = 22
22 * 10 = 220.
@AkilaPalanimuthu: shouldn't 229 be rounded up to 230?
229 is just input number..
Incase of 229.5 it can be rounded to 230 and then can apply the logic
Incase of 229.5 it can be rounded to 230 and then can apply the logic
That's not what I meant. Maybe I read the question wrong but 229 is closer to 230 than to 220.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That was actually my first comment but then I found that code-wise it needed some enhancements, hence my second comment. Also just for fun adding the possibility to use another number than 10. The Math.Round documentation explains that by default 4.5 is rounded down to 4, so even though the OP specified it would be rare, I included the 'MidpointRounding.AwayFrom Zero' argument for what I shamelessly assume to be mathematically 'normal' behaviour.
Open in new window