Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer round in java

Tags:

java

In Java, I want to round up to the closest integer value eliminating the last 2 digits

from 54321 -> 54300 or 8765 -> 8800

like image 826
Cata Lin Avatar asked Jun 25 '26 05:06

Cata Lin


1 Answers

Assuming you want to round 8550 to 8600, just use:

int rounded = ((original + 50) / 100) * 100;

The division will just truncate, but the previous addition will bias that truncation to give an overall round-up effect.

EDIT: As noted by Peter, you need to consider negative numbers separately, and probably subtract 50 instead of adding it.

like image 168
Jon Skeet Avatar answered Jun 27 '26 08:06

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!