How do you round an integer to the closest 100? For example, 497 would round to 500, 98 would round to 100, and 1423 would round to 1400.
Asked
Active
Viewed 4,458 times
3
-
2have you tried something? – stinepike Feb 24 '14 at 10:10
-
@StinePike Im new to Java so am unsure on the ways. I looked on the internet but a lot of replies are for decimals or involve rather long ways. I was wondering if there is a simple way to do such a thing? – Feb 24 '14 at 10:12
-
1ok .. it is definitely not bad to ask questions .. but here people like to see what you have tried so far.. it is better to correct your mistakes instead of providing the full answer :) – stinepike Feb 24 '14 at 10:14
-
@StinePike Thanks for that :) You may be able to tell but I joined this yesterday and am still understanding how it all works. Thanks for your advice – Feb 24 '14 at 10:16
3 Answers
10
I'd divide by 100, round, and then multiply again:
int initial = ...;
int rounded = (int) Math.round(initial/100.0) * 100;
Note to divide by 100.0
and not 100
, so you do the division in floating point arithmetic.

Mureinik
- 297,002
- 52
- 306
- 350
-
1And to stick to just integer arithmetic: `int rounded = ((initial + 50) / 100) * 100;` – Phylogenesis Feb 24 '14 at 10:16
-
-
I haven't a java compiler close, but in C# this answer doesn't work. For example, 450 would round to 400. – Bart van Nierop Feb 24 '14 at 10:29
-
@BartvanNierop In Java, Math.round(x) rounds half up. What does it do in C# ? – Peter Lawrey Feb 24 '14 at 10:52
-
@PeterLawrey It does the same. The problem is floating point precision. The example 450 rounds to 400, but 550 correctly rounds to 600. – Bart van Nierop Feb 24 '14 at 11:01
-
@BartvanNierop Java isn't C#. This works as expected in on both JDK 6 & 7 I have installed. – Mureinik Feb 24 '14 at 12:31
-
@Mureinik Floating point arithmetic has precision issues in every language. – Bart van Nierop Feb 24 '14 at 12:32
-
1
-
@BartvanNierop If C# uses the hardware floating point, it should do the same as Java and so 450 would be rounded to 500. See http://ideone.com/EXkbYM – Peter Lawrey Feb 24 '14 at 17:48
-
1@PeterLawrey I guess it doesn't. See http://ideone.com/ITQmUW and http://ideone.com/k0UVRf. Also, see http://ideone.com/09TtII for the solution failing in Java. – Bart van Nierop Feb 24 '14 at 22:42
-
@BartvanNierop +1 for you and +1 for ideone. double only has 15-17 digits of accuracy, you should be able to find failing values around 2^^60 I would expect. – Peter Lawrey Feb 25 '14 at 09:01
1
Another way, that avoids floating point arithmetic and possible precision errors is something along these lines:
int value = 497;
int rounded = 0;
int remainder = value % 100;
if (remainder >= 50) {
rounded = value - remainder + 100;
} else {
rounded = value - remainder;
}
or simpler:
int rounded = ((value + 50) / 100) * 100;

Bart van Nierop
- 4,130
- 2
- 28
- 32
0
- Divide by 100
- Round
- Multiply by 100
Small demo:
int[] values = { 497, 98, 1423 };
for (int value : values) {
int rounded = (int) Math.round(value / 100.0) * 100;
System.out.format("Before: %4d Rounded: %4d%n", value, rounded);
}

Keppil
- 45,603
- 8
- 97
- 119