Java Round Up Any Number

Java Round up Any Number

Math.ceil() is the correct function to call. I'm guessing a is an int, which would make a / 100 perform integer arithmetic. Try Math.ceil(a / 100.0) instead.

int a = 142;
System.out.println(a / 100);
System.out.println(Math.ceil(a / 100));
System.out.println(a / 100.0);
System.out.println(Math.ceil(a / 100.0));
System.out.println((int) Math.ceil(a / 100.0));

Outputs:

1
1.0
1.42
2.0
2

See http://ideone.com/yhT0l

How to round up to the next integer?

No, Math.ceil() won't work on its own because the problem occurs earlier. a and b are both integers, so dividing them evaluates to an integer which is the floor of the actual result of the division. For a = 3 and b = 2, the result is 1. The ceiling of one is also one - hence you won't get the desired result.

You must fix your division first. By casting one of the operands to a floating point number, you get a non-integer result as desired. Then you can use Math.ceil to round it up. This code should work:

Math.ceil((double)a / b);

How to round a number to n decimal places in Java

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output.

Example:

DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) {
Double d = n.doubleValue();
System.out.println(df.format(d));
}

gives the output:

12
123.1235
0.23
0.1
2341234.2125

EDIT: The original answer does not address the accuracy of the double values. That is fine if you don't care much whether it rounds up or down. But if you want accurate rounding, then you need to take the expected accuracy of the values into account. Floating point values have a binary representation internally. That means that a value like 2.7735 does not actually have that exact value internally. It can be slightly larger or slightly smaller. If the internal value is slightly smaller, then it will not round up to 2.7740. To remedy that situation, you need to be aware of the accuracy of the values that you are working with, and add or subtract that value before rounding. For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:

Double d = n.doubleValue() + 1e-6;

To round down, subtract the accuracy.

Rounding up a number to nearest multiple of 5

int roundUp(int n) {
return (n + 4) / 5 * 5;
}

Note - YankeeWhiskey's answer is rounding to the closest multiple, this is rounding up. Needs a modification if you need it to work for negative numbers. Note that integer division followed by integer multiplication of the same number is the way to round down.

Always round up to ten in Java

See my previous answer to another question for how to handle negative numbers.

To round down to nearest multiple of 10, simply divide by 10 and multiply by 10, using the fact that integer division always rounds down (towards zero).

roundDown = value / 10 * 10;

To round up, add 9 before rounding down.

roundUp = (value + 9) / 10 * 10;

To round half-up, add 5 before rounding down.

roundHalfUp = (value + 5) / 10 * 10;

Test

System.out.println("    Down  Up  Half-Up");
int[] arr = { 32, 25, 88, 50 };
for (int v : arr)
System.out.printf("%2d %4d %4d %5d%n", v, v / 10 * 10, (v + 9) / 10 * 10, (v + 6) / 10 * 10);

Output

    Down  Up  Half-Up
32 30 40 30
25 20 30 30
88 80 90 90
50 50 50 50

round up to 2 decimal places in java?

Well this one works...

double roundOff = Math.round(a * 100.0) / 100.0;

Output is

123.14

Or as @Rufein said

 double roundOff = (double) Math.round(a * 100) / 100;

this will do it for you as well.



Related Topics



Leave a reply



Submit