四舍五入到最近的一百个

我参与了我的java程序,我需要将其舍入到最接近的百位,并认为可能有一些方法可以做到,但我猜不是。 所以我搜索了网上的例子或任何答案,我还没有找到任何答案,因为所有的例子似乎都是近一百个。 我只是想做这个并且向上舍入。 也许有一些我忽略的简单解决方案。 我已经尝试过Math.ceil和其他function,但还没有找到答案。 如果有人能帮我解决这个问题,我将不胜感激。

如果我的数字是203,我希望结果四舍五入为300.你明白了。

  1. 801-> 900
  2. 99-> 100
  3. 14-> 100
  4. 452-> 500

利用整数除法,它会截断商的小数部分。 为了使它看起来像是四舍五入,先添加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 

相关的Java语言规范引用,第15.17.2节 :

整数除法向0舍入。也就是说,在二进制数字提升(第5.6.2节)之后为操作数n和d生成的商是整数值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; } 
 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 

尝试这个:

 (int) (Math.ceil(number/100.0))*100 
 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 

Long数据类型用于确保整数范围的限制不会对较大的值造成任何问题。 例如,在金额值(银行业务领域)的情况下,这可能非常重要。

我没有声名义对OC的答案添加评论,但我认为它应该是:

 ` 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

 A simple implementation of rgettman which gives: roudUp(56007)=60000 roudUp(4511)=5000 roudUp(1000)=1000 roudUp(867)=900 roudUp(17)=20 roudUp(5)=10 roudUp(0)=0 import java.util.*; public class Main { 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("roudUp(56007)="+roundUp(56007)); System.out.println("roudUp(4511)="+roundUp(4511)); System.out.println("roudUp(1000)="+roundUp(1000)); System.out.println("roudUp(867)="+roundUp(867)); System.out.println("roudUp(17)="+roundUp(17)); System.out.println("roudUp(5)="+roundUp(5)); System.out.println("roudUp(0)="+roundUp(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 

下面的代码适用于我将整数舍入到下一个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); } }