Rounding Bigdecimal values with 2 Decimal Places

后端 未结 4 831
太阳男子
太阳男子 2020-12-15 15:44

I want a function to convert Bigdecimal 10.12 for 10.12345 and 10.13 for 10.12556. But no function is satisfying both conversion in same t

相关标签:
4条回答
  • 2020-12-15 16:14

    Add 0.001 first to the number and then call setScale(2, RoundingMode.ROUND_HALF_UP)

    Code example:

    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("10.12445").add(new BigDecimal("0.001"));
        BigDecimal b = a.setScale(2, BigDecimal.ROUND_HALF_UP);
        System.out.println(b);
    }
    
    0 讨论(0)
  • 2020-12-15 16:22

    I think that the RoundingMode you are looking for is ROUND_HALF_EVEN. From the javadoc:

    Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it's even. Note that this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations.

    Here is a quick test case:

    BigDecimal a = new BigDecimal("10.12345");
    BigDecimal b = new BigDecimal("10.12556");
    
    a = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
    b = b.setScale(2, BigDecimal.ROUND_HALF_EVEN);
    
    System.out.println(a);
    System.out.println(b);
    

    Correctly prints:

    10.12
    10.13
    

    UPDATE:

    setScale(int, int) has not been recommended since Java 1.5, when enums were first introduced, and was finally deprecated in Java 9. You should now use setScale(int, RoundingMode) e.g:

    setScale(2, RoundingMode.HALF_EVEN)

    0 讨论(0)
  • 2020-12-15 16:29

    You may try this:

    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("10.12345");
        System.out.println(toPrecision(a, 2));
    }
    
    private static BigDecimal toPrecision(BigDecimal dec, int precision) {
        String plain = dec.movePointRight(precision).toPlainString();
        return new BigDecimal(plain.substring(0, plain.indexOf("."))).movePointLeft(precision);
    }
    

    OUTPUT:

    10.12
    
    0 讨论(0)
  • 2020-12-15 16:33

    You can call setScale(newScale, roundingMode) method three times with changing the newScale value from 4 to 3 to 2 like

    First case

        BigDecimal a = new BigDecimal("10.12345");
    
        a = a.setScale(4, BigDecimal.ROUND_HALF_UP); 
        System.out.println("" + a); //10.1235
        a = a.setScale(3, BigDecimal.ROUND_HALF_UP); 
        System.out.println("" + a); //10.124
        a = a.setScale(2, BigDecimal.ROUND_HALF_UP);
        System.out.println("" + a); //10.12
    

    Second case

        BigDecimal a = new BigDecimal("10.12556");
    
        a = a.setScale(4, BigDecimal.ROUND_HALF_UP); 
        System.out.println("" + a); //10.1256
        a = a.setScale(3, BigDecimal.ROUND_HALF_UP); 
        System.out.println("" + a); //10.126
        a = a.setScale(2, BigDecimal.ROUND_HALF_UP);
        System.out.println("" + a); //10.13
    
    0 讨论(0)
提交回复
热议问题