Java 四舍五入到最接近的一百
我在java程序中遇到了一个部分,我需要把它凑到最接近的一百,我想可能有办法做到这一点,但我想没有。所以我在网上搜索了一些例子或任何答案,但我还没有找到任何,因为所有的例子似乎都接近100个。我只想做这件事,然后把它汇总起来。也许我忽略了一些简单的解决方案。我尝试了Java 四舍五入到最接近的一百,java,math,rounding,Java,Math,Rounding,我在java程序中遇到了一个部分,我需要把它凑到最接近的一百,我想可能有办法做到这一点,但我想没有。所以我在网上搜索了一些例子或任何答案,但我还没有找到任何,因为所有的例子似乎都接近100个。我只想做这件事,然后把它汇总起来。也许我忽略了一些简单的解决方案。我尝试了Math.ceil和其他函数,但到目前为止还没有找到答案。如果有人能帮我解决这个问题,我将不胜感激 如果我的数字是203,我希望结果四舍五入为300。你明白了 801->900 99->100 14->100 452->500 利用整
Math.ceil
和其他函数,但到目前为止还没有找到答案。如果有人能帮我解决这个问题,我将不胜感激
如果我的数字是203,我希望结果四舍五入为300。你明白了
利用整数除法,它会截断商的小数部分。要使其看起来像是四舍五入,请先添加99
int rounded = ((num + 99) / 100 ) * 100;
示例:
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
相关:
整数除法向0舍入。也就是说,为
二进制数字提升后的整数操作数n和d
(§5.6.2)是一个整数值q,其大小尽可能大
同时满足| d·q |≤ |n |
这里有一个算法,我相信它适用于任何“倍数”的情况。让我知道你的想法
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;
}
我没有足够的声誉来评论他的回答,但我认为应该是:
`
if (number % multiple != 0) {
int division = (number / multiple) + 1;
result = division * multiple;
} else {
result = Math.max(multiple, number);
}
`
使用else
,例如round(9,3)=9
,否则它将是round(9,3)=3
long i = 2147483648L;
if(i % 100 != 0) {
long roundedI = (100 - (i % 100)) + i;
}
例如:
649: (100 - (649 % 100)) + 649 -> (100 - 49) + 649) -> 51 + 649 = 700
985: (100 - (985 % 100)) + 985 -> (100 - 85) + 985) -> 15 + 985 = 1000
长数据类型用于确保整数范围的限制不会对较大的值造成任何问题。例如,对于金额值(银行领域),这可能非常重要
TargetMan 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));
}
}
输出:
roundUp(56007) = 60000
roundUp(4511) = 5000
roundUp(1000) = 1000
roundUp(867) = 900
roundUp(17) = 20
roundUp(5) = 10
roundUp(0) = 0
另一种方法是使用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
试试这个:
(int) (Math.ceil(number/100.0))*100
下面的代码用于将整数舍入到下一个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);
}
}
这对我来说非常有效:
var round100 = function(n) {
if (n < 50) {
var low = n - (n % 100);
return Math.round(low);
}
return Math.round(n/100) * 100;
}
var round100=函数(n){
如果(n<50){
风险值低=n-(n%100);
返回Math.round(低);
}
返回数学整数(n/100)*100;
}
您可以将var(变量)分配给任何对象。int value=0;
int value = 0;
for (int i = 100; i <= Math.round(total); i = i + 100) {
if (i < Math.round(total)) {
value = i;
}
}
对于(int i=100;我哇,我从来没有想过利用这样的截断。这个答案非常棒。非常感谢你教我一些东西!*旁注---如果你使用浮点值,而不是使用int
,大多数语言都以某种方式支持楼层函数。@DaSh Yes it w因为0
是100
和((0+99)/100)的最近倍数,所以将0
四舍五入到最接近的百是0
* 100 < <代码> > 99/100×100 > > >代码> 0*100 < /代码>=代码> 0 < /代码> .@ rgttman优秀!简单而优雅。我试图为这做一些方程式,但失败了。张贴一段代码没有多大帮助,你应该考虑给你的答案加上一些解释。谢谢你为我工作。rst行应为:int result=number;
int value = 0;
for (int i = 100; i <= Math.round(total); i = i + 100) {
if (i < Math.round(total)) {
value = i;
}
}