Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigDecimal round to 0 or 5

I'm tinkering with BigDecimal and currency formatting in Android, and was wondering if it was possible to do the following using BigDecimal:

What I desire:

  64.99 --> 65.00 (Rounded Up)
  64.99 --> 60.00 (Rounded Down)
  65.01 --> 70.00 (Rounded Up)
  65.01 --> 65.00 (Rounded Down)

At present, with my code below, I'm only able to round to zeros. For example:

What I get:

  64.99 --> 70.00 (Rounded Up)
  64.99 --> 60.00 (Rounded Down)
  65.01 --> 70.00 (Rounded Up)
  65.01 --> 60.00 (Rounded Down)

Is there a way using BigDecimal to achieve what I desire?

My code:

private static void printRoundedValues() {
    NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
    BigDecimal valueUp = new BigDecimal(64.50, new MathContext(1,RoundingMode.UP));
    BigDecimal valueDown = new BigDecimal(64.50, new MathContext(1,RoundingMode.DOWN)); 

    System.out.println("Value Up: " +  currencyFormat.format(valueUp));
    System.out.println("Value Down: " +  currencyFormat.format(valueDown));
}
like image 279
TEK Avatar asked Feb 19 '14 17:02

TEK


People also ask

How do you round numbers in BigDecimal?

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.

What is the value of BigDecimal zero?

BigDecimal is zero means that it can store 0, 0.0 or 0.00 etc values.

What is the range of BigDecimal?

A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point. If less than zero, the unscaled value of the number is multiplied by 10^(-scale).

How do I change precision of BigDecimal in Java?

To change a BigDecimal's precision, use BigDecimal. round(new MathContext(precision, roundingMode)) . To change a BigDecimal's scale, use BigDecimal. setScale(scale, roundingMode) .


1 Answers

You can multiply by 2, then round to the nearest 10, then divide by 2. The precision is 2 for these BigDecimals; it may need to be different for BigDecimals of different scale.

BigDecimal[] bds = {new BigDecimal("64.99"), new BigDecimal("65.01")};
BigDecimal two = new BigDecimal("2");
MathContext mcUp = new MathContext(2, RoundingMode.UP);
MathContext mcDown = new MathContext(2, RoundingMode.DOWN);
NumberFormat currency = NumberFormat.getCurrencyInstance();
for (BigDecimal bd : bds)
{
    System.out.println("Test: " + bd);
    BigDecimal roundUp5 = bd.multiply(two).round(mcUp).divide(two);
    System.out.println("Round up: " + currency.format(roundUp5));
    BigDecimal roundDown5 = bd.multiply(two).round(mcDown).divide(two);
    System.out.println("Round down: " + currency.format(roundDown5));
}

Output:

Test: 64.99
Round up: $65.00
Round down: $60.00
Test: 65.01
Round up: $70.00
Round down: $65.00
like image 94
rgettman Avatar answered Sep 29 '22 18:09

rgettman