Reputation: 937
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.
Upvotes: 35
Views: 57796
Reputation: 323
I don't have enought reputation to add a comment to O.C.'s answer but I think it should be:
if (number % multiple != 0) {
int division = (number / multiple) + 1;
result = division * multiple;
} else {
result = Math.max(multiple, number);
}
with the else
so that, for example round(9, 3) = 9
, otherwise it would be round(9, 3) = 3
Upvotes: 1
Reputation: 1
int value = 0;
for (int i = 100; i <= Math.round(total); i = i + 100) {
if (i < Math.round(total)) {
value = i;
}
}
It will run until total is == to i and i will be increased every time
Upvotes: -1
Reputation: 59
A simple implementation of rgettman trunaction:
public class Main {
private static int roundUp(int src) {
int len = String.valueOf(src).length() - 1;
if (len == 0) len = 1;
int d = (int) Math.pow((double) 10, (double) len);
return (src + (d - 1)) / d * d;
}
public static void main(String[] args) {
System.out.println("roundUp(56007) = " + roundUp(56007));
System.out.println("roundUp(4511) = " + roundUp(4511));
System.out.println("roundUp(1000) = " + roundUp(1000));
System.out.println("roundUp(867) = " + roundUp(867));
System.out.println("roundUp(17) = " + roundUp(17));
System.out.println("roundUp(5) = " + roundUp(5));
System.out.println("roundUp(0) = " + roundUp(0));
}
}
Output:
roundUp(56007) = 60000
roundUp(4511) = 5000
roundUp(1000) = 1000
roundUp(867) = 900
roundUp(17) = 20
roundUp(5) = 10
roundUp(0) = 0
Upvotes: 3
Reputation: 1
This worked perfectly for me:
var round100 = function(n) {
if (n < 50) {
var low = n - (n % 100);
return Math.round(low);
}
return Math.round(n/100) * 100;
}
You can assign your var (variables) to anything.
Upvotes: 0
Reputation: 1
The below code works for me to round an integer to the next 10 or 100 or 500 or 1000 etc.
public class MyClass {
public static void main(String args[]) {
int actualValue = 34199;
int nextRoundedValue = 500 // change this based on your round requirment ex: 10,100,500,...
int roundedUpValue = actualValue;
//Rounding to next 500
if(actualValue%nextRoundedValue != 0)
roundedUpValue =
(((actualValue/nextRoundedValue)) * nextRoundedValue) + nextRoundedValue;
System.out.println(roundedUpValue);
}
}
Upvotes: 0
Reputation: 1027
One other way is to use BigDecimal
private static double round(double number, int precision, RoundingMode roundingMode) {
BigDecimal bd = null;
try {
bd = BigDecimal.valueOf(number);
} catch (NumberFormatException e) {
// input is probably a NaN or infinity
return number;
}
bd = bd.setScale(precision, roundingMode);
return bd.doubleValue();
}
round(102.23,0,RoundingMode.UP) = 103
round(102.23,1,RoundingMode.UP) = 102.3
round(102.23,2,RoundingMode.UP) = 102.24
round(102.23,-1,RoundingMode.UP) = 110
round(102.23,-2,RoundingMode.UP) = 200
round(102.23,-3,RoundingMode.UP) = 1000
Upvotes: 4
Reputation: 151
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).
Upvotes: 4
Reputation: 51
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
Upvotes: 5
Reputation: 178263
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|.
Upvotes: 68
Reputation: 6819
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;
}
Upvotes: 12