开发者

How to round integer in java

开发者 https://www.devze.com 2023-02-28 17:47 出处:网络
I want to round the number 1732 to the nearest ten, hundred and thousand. I tried with Math round functions, but it was writte开发者_运维问答n only for float and double. How to do this for Integer? Is

I want to round the number 1732 to the nearest ten, hundred and thousand. I tried with Math round functions, but it was writte开发者_运维问答n only for float and double. How to do this for Integer? Is there any function in java?


What rounding mechanism do you want to use? Here's a primitive approach, for positive numbers:

int roundedNumber = (number + 500) / 1000 * 1000;

This will bring something like 1499 to 1000 and 1500 to 2000.

If you could have negative numbers:

int offset = (number >= 0) ? 500 : -500;
int roundedNumber = (number + offset) / 1000 * 1000;


(int)(Math.round( 1732 / 10.0) * 10)

Math.round(double) takes the double and then rounds up as an nearest integer. So, 1732 will become 173.2 (input parameter) on processing by Math.round(1732 / 10.0). So the method rounds it like 173.0. Then multiplying it with 10 (Math.round( 1732 / 10.0) * 10) gives the rounded down answer, which is 173.0 will then be casted to int.


Use Precision (Apache Commons Math 3.1.1)

Precision.round(double, scale); // return double
Precision.round(float, scale); // return float

Use MathUtils (Apache Commons Math) - Older versions

MathUtils.round(double, scale); // return double
MathUtils.round(float, scale); // return float

scale - The number of digits to the right of the decimal point. (+/-)


Discarded because method round(float, scale) be used.

Math.round(MathUtils.round(1732, -1)); // nearest ten, 1730
Math.round(MathUtils.round(1732, -2)); // nearest hundred, 1700
Math.round(MathUtils.round(1732, -3)); // nearest thousand, 2000


Better solution

int i = 1732;
MathUtils.round((double) i, -1); // nearest ten, 1730.0
MathUtils.round((double) i, -2); // nearest hundred, 1700.0
MathUtils.round((double) i, -3); // nearest thousand, 2000.0


You could try:

int y = 1732;
int x = y - y % 10;

The result will be 1730.

Edit: This doesn't answer the question. It simply removes part of the number but doesn't "round to the nearest".


At nearest ten:

int i = 1986;
int result;

result = i%10 > 5 ? ((i/10)*10)+10 : (i/10)*10;

(Add zero's at will for hundred and thousand).


why not just check the unit digit... 1. if it is less than or equal to 5, add 0 at the unit position and leave the number as it is. 2. if it is more than 5, increment the tens digit, add 0 at the unit position.

ex: 1736 (since 6 >=5) the rounded number will be 1740. now for 1432 (since 2 <5 ) the rounded number will be 1430....

I hope this will work... if not than let me know about those cases...

Happy Programming,


very simple. try this

int y = 173256457;int x = (y/10)*10; 

Now in this you can replace 10 by 100,1000 and so on....


Its very easy..

int x = 1234; int y = x - x % 10; //It will give 1230

int y = x - x % 100; //It will give 1200

int y = x - x % 1000; //It will give 1000

The above logic will just convert the last digits to 0. If you want actual round of// For eg. 1278 this should round off to 1280 because last digit 8 > 5 for this i wrote a function check it out.

 private double returnAfterRoundDigitNum(double paramNumber, int noOfDigit)  
    {  
     double tempSubtractNum = paramNumber%(10*noOfDigit);  
     double tempResultNum = (paramNumber - tempSubtractNum);  
     if(tempSubtractNum >= (5*noOfDigit))  
      {  
          tempResultNum = tempResultNum + (10*noOfDigit);  
      }  
      return tempResultNum;  
   }  

Here pass 2 parameters one is the number and the other is position till which you have to round off.

Regards, Abhinav


I usually do it this way:

int num = 1732;
int roundedNum = Math.round((num + 9)/10 * 10);

This will give you 1740 as the result.

Hope this will help.


    int val2 = 1732;
    val2 = (int)(Math.rint((double) i / 10) * 10);

The output is:1730


Have you looked at the implementation of Mathutils.round() ? It's all based on BigDecimal and string conversions. Hard to imagine many approaches that are less efficient.


Without using any math utils, rounding could be achieved to any unit as below:

double roundValue (double input, double toNearest){
//toNearest is any rounding base like 10, 100 or 1000.
    double modValue = input % toNearest;
    System.out.println(modValue);
    if(modValue == 0d){
        roundedValue = input;
    }
    else
    {
        roundedValue = ((input - modValue) + toNearest);
    }
    System.out.println(roundedValue);
    return roundedValue;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消