java - BigDecimal

Posted by Mk12 on Stack Overflow See other posts from Stack Overflow or by Mk12
Published on 2009-08-31T23:14:30Z Indexed on 2010/03/15 17:59 UTC
Read the original article Hit count: 400

Filed under:
|
|

I was trying to make my own class for currencies using longs, but Apparently I should use BigDecimal (and then whenever I print it just add the $ sign before it). Could someone please get me started? What would be the best way to use BigDecimals for Dollar currencies, like making it at least but no more than 2 decimal places for the cents, etc. The api for BigDecimal is huge, and I don't know which methods to use. Also, BigDecimal has better precision, but isn't that all lost if it passes through a double? if I do new BigDecimal(24.99), how will it be different than using a double? Or should I use the constructor that uses a String instead?

EDIT: I decided to use BigDecimals, and then use:

private static final java.text.NumberFormat moneyt =
        java.text.NumberFormat.getCurrencyInstance();
{
    money.setRoundingMode(RoundingMode.HALF_EVEN);
}

and then whenever I display the BigDecimals, to use money.format(theBigDecimal). Is this alright? Should I have the BigDecimal rounding it too? Because then it doesn't get rounded before it does another operation.. if so, could you show me how? And how should I create the BigDecimals? new BigDecimal("24.99") ?

Well, after many comments to Vineet Reynolds (thanks for keeping coming back and answering), this is what I have decided. I use BigDecimals and a NumberFormat. Here is where I create the NumberFormat instance.

private static final NumberFormat money;
static {
    money = NumberFormat.getCurrencyInstance(Locale.CANADA);
    money.setRoundingMode(RoundingMode.HALF_EVEN);
}

Here is my BigDecimal:

private final BigDecimal price;

Whenever I want to display the price, or another BigDecimal that I got through calculations of price, I use:

money.format(price)

to get the String. Whenever I want to store the price, or a calculation from price, in a database or in a field or anywhere, I use (for a field):

myCalculatedResult = price.add(new BigDecimal("34.58")).setScale(2, RoundingMode.HALF_EVEN);

.. but I'm thinking now maybe I should not have the NumberFormat round, but when I want to display do this:

System.out.print(money.format(price.setScale(2, RoundingMode.HALF_EVEN);

That way to ensure the model and things displayed in the view are the same. I don't do:

price = price.setScale(2, RoundingMode.HALF_EVEN);

Because then it would always round to 2 decimal places and wouldn't be as precise in calculations.

So its all solved now, I guess. But is there any shortcut to typing calculatedResult.setScale(2, RoundingMode.HALF_EVEN) all the time? All I can think of is static importing HALF_EVEN...

EDIT: I've changed my mind a bit, I think if I store a value, I won't round it unless I have no more operations to do with it, e.g. if it was the final total that will be charged to someone. I will only round things at the end, or whenever necessary, and I will still use NumberFormat for the currency formatting, but since I always want rounding for display, I made a static method for display:

public static String moneyFormat(BigDecimal price) {
    return money.format(price.setScale(2, RoundingMode.HALF_EVEN));
}

So values stored in variables won't be rounded, and I'll use that method to display prices.

© Stack Overflow or respective owner

Related posts about java

Related posts about bigdecimal