Java 四舍五入到最接近的一百
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18407634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding Up To The Nearest Hundred
提问by Tastybrownies
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.
我来到了我的 java 程序的一部分,我需要四舍五入到最接近的一百,并认为可能有某种方法可以做到这一点,但我想不是。所以我在网上搜索了例子或任何答案,但我还没有找到任何答案,因为所有的例子似乎都是最接近的一百。我只是想这样做并四舍五入。也许有一些我忽略的简单解决方案。我已经尝试过Math.ceil
和其他功能,但到目前为止还没有找到答案。如果有人能帮助我解决这个问题,我将不胜感激。
If my number is 203, I want the result rounded to be 300. You get the point.
如果我的数字是 203,我希望结果四舍五入为 300。你明白了。
- 801->900
- 99->100
- 14->100
- 452->500
- 801->900
- 99->100
- 14->100
- 452->500
采纳答案by rgettman
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.
利用整数除法,它会截断商的小数部分。为了使它看起来像是四舍五入,请先添加 99。
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|.
整数除法向 0 舍入。也就是说,二进制数值提升(第 5.6.2 节)后为整数的操作数 n 和 d 产生的商是整数值 q,其大小在满足 |d·q| 的同时尽可能大。≤ |n|。
回答by O.C.
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;
}
回答by Pradeepcm
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
回答by negste
I don't have enought reputation to add a comment to O.C.'s answer but I think it should be:
我没有足够的声誉来为OC的答案添加评论,但我认为它应该是:
`
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
与else
这样,例如round(9, 3) = 9
,否则它将是round(9, 3) = 3
回答by Mansingh Shitole
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).
Long 数据类型用于确保整数范围的限制不会对较大的值造成任何问题。例如,这在金额值(银行域)的情况下可能非常重要。
回答by sevaggelinos
A simple implementation of rgettman trunaction:
rgettman 截断的简单实现:
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
回答by vanval
One other way is to use BigDecimal
另一种方法是使用 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
回答by user2859809
Try this:
尝试这个:
(int) (Math.ceil(number/100.0))*100
回答by Mahesh
The below code works for me to round an integer to the next 10 or 100 or 500 or 1000 etc.
下面的代码适用于我将整数四舍五入到下一个 10 或 100 或 500 或 1000 等。
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);
}
}
回答by TaeterTot
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.
您可以将您的 var(变量)分配给任何东西。