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));
}