Java BigDecimal: Round to the nearest whole value

后端 未结 6 725
滥情空心
滥情空心 2020-11-27 16:41

I need the following results

100.12 -> 100.00
100.44 -> 100.00
100.50 -> 101.00
100.75 -> 101.00

.round() or

相关标签:
6条回答
  • 2020-11-27 17:18

    Here's an awfully complicated solution, but it works:

    public static BigDecimal roundBigDecimal(final BigDecimal input){
        return input.round(
            new MathContext(
                input.toBigInteger().toString().length(),
                RoundingMode.HALF_UP
            )
        );
    }
    

    Test Code:

    List<BigDecimal> bigDecimals =
        Arrays.asList(new BigDecimal("100.12"),
            new BigDecimal("100.44"),
            new BigDecimal("100.50"),
            new BigDecimal("100.75"));
    for(final BigDecimal bd : bigDecimals){
        System.out.println(roundBigDecimal(bd).toPlainString());
    }
    

    Output:

    100
    100
    101
    101

    0 讨论(0)
  • 2020-11-27 17:26

    Simply look at:

    http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#ROUND_HALF_UP

    and:

    setScale(int precision, int roundingMode)
    

    Or if using Java 6, then

    http://download.oracle.com/javase/6/docs/api/java/math/RoundingMode.html#HALF_UP

    http://download.oracle.com/javase/6/docs/api/java/math/MathContext.html

    and either:

    setScale(int precision, RoundingMode mode);
    round(MathContext mc);
    
    0 讨论(0)
  • 2020-11-27 17:27

    You want

    round(new MathContext(0));  // or perhaps another math context with rounding mode HALF_UP
    
    0 讨论(0)
  • 2020-11-27 17:31

    If i go by Grodriguez's answer

    System.out.println("" + value);
    value = value.setScale(0, BigDecimal.ROUND_HALF_UP);
    System.out.println("" + value);
    

    This is the output

    100.23 -> 100
    100.77 -> 101
    

    Which isn't quite what i want, so i ended up doing this..

    System.out.println("" + value);
    value = value.setScale(0, BigDecimal.ROUND_HALF_UP);
    value = value.setScale(2, BigDecimal.ROUND_HALF_UP);
    System.out.println("" + value);
    

    This is what i get

    100.23 -> 100.00
    100.77 -> 101.00
    

    This solves my problem for now .. : ) Thank you all.

    0 讨论(0)
  • 2020-11-27 17:32

    You can use setScale() to reduce the number of fractional digits to zero. Assuming value holds the value to be rounded:

    BigDecimal scaled = value.setScale(0, RoundingMode.HALF_UP);
    System.out.println(value + " -> " + scaled);
    

    Using round() is a bit more involved as it requires you to specify the number of digits to be retained. In your examples this would be 3, but this is not valid for all values:

    BigDecimal rounded = value.round(new MathContext(3, RoundingMode.HALF_UP));
    System.out.println(value + " -> " + rounded);
    

    (Note that BigDecimal objects are immutable; both setScale and round will return a new object.)

    0 讨论(0)
  • 2020-11-27 17:34

    I don't think you can round it like that in a single command. Try

        ArrayList<BigDecimal> list = new ArrayList<BigDecimal>();
        list.add(new BigDecimal("100.12"));
        list.add(new BigDecimal("100.44"));
        list.add(new BigDecimal("100.50"));
        list.add(new BigDecimal("100.75"));
    
        for (BigDecimal bd : list){
            System.out.println(bd+" -> "+bd.setScale(0,RoundingMode.HALF_UP).setScale(2));
        }
    
    Output:
    100.12 -> 100.00
    100.44 -> 100.00
    100.50 -> 101.00
    100.75 -> 101.00
    

    I tested for the rest of your examples and it returns the wanted values, but I don't guarantee its correctness.

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