How to round integer in java

后端 未结 12 776
野趣味
野趣味 2020-12-09 09:19

I want to round the number 1732 to the nearest ten, hundred and thousand. I tried with Math round functions, but it was written only for float and double. How to do this for

相关标签:
12条回答
  • 2020-12-09 09:44

    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 讨论(0)
  • 2020-12-09 09:45

    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
    
    0 讨论(0)
  • 2020-12-09 09:46

    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

    0 讨论(0)
  • 2020-12-09 09:50
        int val2 = 1732;
        val2 = (int)(Math.rint((double) i / 10) * 10);
    

    The output is:1730

    0 讨论(0)
  • 2020-12-09 09:52

    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;
    
    0 讨论(0)
  • 2020-12-09 09:53

    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,

    0 讨论(0)
提交回复
热议问题