# How do I round a number to nearest 2.5 multiple?

Printable View

• 11-24-2012, 11:50 PM
LasseA
How do I round a number to nearest 2.5 multiple?
I'v having a program that supports calculating weight in kg for a training program(powerlifting).
When it prints the numbers I get "119.69999999999999" and "136.8" etc. I want those to be "120" and "137,5", since you can't load the barbell with such kg-amounts in any gym I've been to.
So I need to put in a code that rounds up/down to nearest 2,5kg multiple.
Been trying to search for it, but can't seem to find answers for anything but rounding to an integer value.
• 11-25-2012, 12:46 AM
Fubarable
Re: How do I round a number to nearest 2.5 multiple?
One way is to multiply your number by a number that will allow rounding by Math.round(...) to do the correct rounding, and then divide by that number before using String.format(...) or DecimalFormat to produce a nice String representation. You could multiply the number by 0.4 or divide it by 2.5, then round it to the nearest int, then divide the result by 0.4 or multiply it by 2.5 to get the result, but then use a String formatting tool to round this to 1 decimal place. String.format("%.1f", myNumber) could work well for this.
• 11-25-2012, 01:11 PM
LasseA
Re: How do I round a number to nearest 2.5 multiple?
Cool, thanks for answer, will try those suggestions :)