Rounding Up To The Nearest Hundred

45,761

Solution 1

Take advantage of integer division, which truncates the decimal portion of the quotient. To make it look like it's rounding up, add 99 first.

int rounded = ((num + 99) / 100 ) * 100;

Examples:

801: ((801 + 99) / 100) * 100 → 900 / 100 * 100 → 9 * 100 = 900
99 : ((99 + 99) / 100) * 100 → 198 / 100 * 100 → 1 * 100 = 100
14 : ((14 + 99) / 100) * 100 → 113 / 100 * 100 → 1 * 100 = 100
452: ((452 + 99) / 100) * 100 → 551 / 100 * 100 → 5 * 100 = 500
203: ((203 + 99) / 100) * 100 → 302 / 100 * 100 → 3 * 100 = 300
200: ((200 + 99) / 100) * 100 → 299 / 100 * 100 → 2 * 100 = 200

Relevant Java Language Specification quote, Section 15.17.2:

Integer division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d · q| ≤ |n|.

Solution 2

Here is an algorithm which I belive works for any "multiple of" case. Let me know what you think.

int round (int number,int multiple){

    int result = multiple;

    //If not already multiple of given number

    if (number % multiple != 0){

        int division = (number / multiple)+1;

        result = division * multiple;

    }

    return result;

}

Solution 3

Try this:

(int) (Math.ceil(number/100.0))*100

Solution 4

int roundUpNumberByUsingMultipleValue(double number, int multiple) {

        int result = multiple;

        if (number % multiple == 0) {
            return (int) number;
        }

        // If not already multiple of given number

        if (number % multiple != 0) {

            int division = (int) ((number / multiple) + 1);

            result = division * multiple;

        }
        return result;

    }

Example:
System.out.println("value 1 =" + round(100.125,100));   
System.out.println("value 2 =" + round(163,50));
System.out.println("value 3 =" + round(200,100));
System.out.println("value 4 =" + round(235.33333333,100));
System.out.println("value 5 =" + round(0,100));

OutPut: 
value 1 =200
value 2 =200
value 3 =200
value 4 =300
value 5 =0

Solution 5

long i = 2147483648L;
if(i % 100 != 0) {
   long roundedI = (100 - (i % 100)) + i;
}

Example:

649: (100 - (649 % 100)) + 649 -> (100 - 49) + 649) -> 51 + 649 = 700
985: (100 - (985 % 100)) + 985 -> (100 - 85) + 985) -> 15 + 985 = 1000

Long datatype is used to make sure the limitation of integer range should not cause any problem for larger values. For ex, this might be very important in case of an amount value (banking domain).

Share:
45,761
Tastybrownies
Author by

Tastybrownies

Undergraduate computer science student, data mining researcher, Cleveland sports fan, and a guy that loves golf, video games, and hanging out with friends.

Updated on July 27, 2022

Comments

  • Tastybrownies
    Tastybrownies almost 2 years

    I came to a part in my java program where I need to round up to the nearest hundred and thought that there was probably some way to do it but I guess not. So I searched the net for examples or any answers and I've yet to find any since all examples appear to be to the nearest hundred. I just want to do this and round UP. Maybe there's some simple solution that I'm overlooking. I have tried Math.ceil and other functions but have not found an answer as of yet. If anyone could help me with this issue I would greatly appreciate it.

    If my number is 203, I want the result rounded to be 300. You get the point.

    1. 801->900
    2. 99->100
    3. 14->100
    4. 452->500