72

I was trying to make my own class for currencies using longs, but apparently I should use BigDecimal instead. Could someone help me get 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?

3
  • 1
    Everything looks fine, but for one final thing - when you use setScale() on a BigDecimal instance, especially if you will use it for addition later, provide the rounding factor of n+1, where n is the number of significant digits. For instance, set a rounding factor of 3, for the following addition 0.043 + 0.043 will yield 0.09 instead of 0.08 (if you chose 2 significant digits for rounding/storage). Sep 1, 2009 at 22:19
  • In the previous example, if you chose 2 significant digits, and performed rounding (as an intermediate operation) before addition, you will get 0.04 + 0.04 = 0.08 Sep 1, 2009 at 22:21
  • Should my variable that stores the percentage to be removed from the price be a BigDecimal also?
    – mk12
    Sep 2, 2009 at 0:00

9 Answers 9

95

Here are a few hints:

  1. Use BigDecimal for computations if you need the precision that it offers (Money values often need this).
  2. Use the NumberFormat class for display. This class will take care of localization issues for amounts in different currencies. However, it will take in only primitives; therefore, if you can accept the small change in accuracy due to transformation to a double, you could use this class.
  3. When using the NumberFormat class, use the scale() method on the BigDecimal instance to set the precision and the rounding method.

PS: In case you were wondering, BigDecimal is always better than double, when you have to represent money values in Java.

PPS:

Creating BigDecimal instances

This is fairly simple since BigDecimal provides constructors to take in primitive values, and String objects. You could use those, preferably the one taking the String object. For example,

BigDecimal modelVal = new BigDecimal("24.455");
BigDecimal displayVal = modelVal.setScale(2, RoundingMode.HALF_EVEN);

Displaying BigDecimal instances

You could use the setMinimumFractionDigits and setMaximumFractionDigits method calls to restrict the amount of data being displayed.

NumberFormat usdCostFormat = NumberFormat.getCurrencyInstance(Locale.US);
usdCostFormat.setMinimumFractionDigits( 1 );
usdCostFormat.setMaximumFractionDigits( 2 );
System.out.println( usdCostFormat.format(displayVal.doubleValue()) );
25
  • 3
    Agree especially on number 2. Keep the view (formatting for the display) separate from the model (BigDecimal). You can always write a custom java.text.Format to handle your specific data type.
    – Steve Kuo
    Sep 1, 2009 at 0:33
  • Don't you mean DecimalFormat, not NumberFormat?
    – mk12
    Sep 1, 2009 at 1:22
  • 8
    -1 for using the BigDecimal(double) constructor .. even though the documentation says that this constructor is 'unpredictable' Sep 1, 2009 at 3:18
  • 1
    Let me rephrase your question as "When should I round a BigDecimal value?" That would depend on how you use the numbers - addition and subtraction require lesser number of decimal values to be stored, whereas multiplication and division would require more. In fact, you need 1 extra decimal place for addition and subtraction (to handle rounding for overflow). Unit costs should not be rounded, and neither should conversion units. All in all, it depends on what you are storing in the BigDecimal instance. If you need to retain precision throughout operations, perform rounding at the end. Sep 1, 2009 at 20:57
  • 1
    Ideally, you should round at the end of the operation, and store that value in the database (usually for auditing in the financial services world), so that the value displayed and the value stored in the database remains the same. So you have your answer there, use Number format just for the currency formatting, and use setScale at the beginning (if necessary) and at the end (before storage). Removal of the rounding operations will enable verification of mathematical correctness of the computation. Sep 1, 2009 at 21:01
41

I would recommend a little research on Money Pattern. Martin Fowler in his book Analysis pattern has covered this in more detail.

public class Money {

    private static final Currency USD = Currency.getInstance("USD");
    private static final RoundingMode DEFAULT_ROUNDING = RoundingMode.HALF_EVEN;

    private final BigDecimal amount;
    private final Currency currency;   

    public static Money dollars(BigDecimal amount) {
        return new Money(amount, USD);
    }

    Money(BigDecimal amount, Currency currency) {
        this(amount, currency, DEFAULT_ROUNDING);
    }

    Money(BigDecimal amount, Currency currency, RoundingMode rounding) {
        this.currency = currency;      
        this.amount = amount.setScale(currency.getDefaultFractionDigits(), rounding);
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public Currency getCurrency() {
        return currency;
    }

    @Override
    public String toString() {
        return getCurrency().getSymbol() + " " + getAmount();
    }

    public String toString(Locale locale) {
        return getCurrency().getSymbol(locale) + " " + getAmount();
    }   
}

Coming to the usage:

You would represent all monies using Money object as opposed to BigDecimal. Representing money as big decimal will mean that you will have the to format the money every where you display it. Just imagine if the display standard changes. You will have to make the edits all over the place. Instead using the Money pattern you centralize the formatting of money to a single location.

Money price = Money.dollars(38.28);
System.out.println(price);
2
  • 4
    One problem with this- not all currencies default to the symbol before the amount, or having a space inbetween. That's things that should be part of the fomatting. May 23, 2016 at 17:15
  • 1
    That's right @GabeSechan - this might interest you drivy.engineering/multi-currency-java
    – Mick
    Oct 22, 2019 at 11:41
12

Or, wait for JSR-354. Java Money and Currency API coming soon!

4
  • 5
    Don't you hate people that check down good advice without leaving any comment! Mar 31, 2015 at 17:56
  • 2
    #James Drinkard That is not the case. JSR 354 is scheduled to be release shortly as an independent library. The original plan was to wait for Java 9, but an interim library will be released soon Apr 26, 2015 at 6:26
  • 2
    Maybe Java 2035?
    – th3byrdm4n
    Nov 13, 2018 at 20:23
  • 3
    What can I say, I'm no longer there spec lead Nov 27, 2018 at 21:40
1

1) If you are limited to the double precision, one reason to use BigDecimals is to realize operations with the BigDecimals created from the doubles.

2) The BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, while the double wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double

3) It should make no difference

You should have no difficulties with the $ and precision. One way to do it is using System.out.printf

1

Use BigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP) when you want to round up to the 2 decimal points for cents. Be aware of rounding off error when you do calculations though. You need to be consistent when you will be doing the rounding of money value. Either do the rounding right at the end just once after all calculations are done, or apply rounding to each value before doing any calculations. Which one to use would depend on your business requirement, but generally, I think doing rounding right at the end seems to make a better sense to me.

Use a String when you construct BigDecimal for money value. If you use double, it will have a trailing floating point values at the end. This is due to computer architecture regarding how double/float values are represented in binary format.

3
  • 11
    Note that for financial apps, ROUND_HALF_EVEN is the most common rounding mode since it avoids bias. Aug 31, 2009 at 23:49
  • Good point about avoiding bias. Didn't know it worked to achieve that. Aug 31, 2009 at 23:54
  • @Michael: Thanks for the tip. I didn't know that. I always wondered how best deal with bias/cumulative errors. Learned something new today. :)
    – tim_wonil
    Sep 1, 2009 at 0:23
1

Primitive numeric types are useful for storing single values in memory. But when dealing with calculation using double and float types, there is a problems with the rounding.It happens because memory representation doesn't map exactly to the value. For example, a double value is supposed to take 64 bits but Java doesn't use all 64 bits.It only stores what it thinks the important parts of the number. So you can arrive to the wrong values when you adding values together of the float or double type.

Please see a short clip https://youtu.be/EXxUSz9x7BM

1

I would be radical. No BigDecimal.

Here is a great article https://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/

Ideas from here.

import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) {
        testConstructors();
        testEqualsAndCompare();
        testArithmetic();
    }

    private static void testEqualsAndCompare() {
        final BigDecimal zero = new BigDecimal("0.0");
        final BigDecimal zerozero = new BigDecimal("0.00");

        boolean zerosAreEqual = zero.equals(zerozero);
        boolean zerosAreEqual2 = zerozero.equals(zero);

        System.out.println("zerosAreEqual: " + zerosAreEqual + " " + zerosAreEqual2);

        int zerosCompare = zero.compareTo(zerozero);
        int zerosCompare2 = zerozero.compareTo(zero);
        System.out.println("zerosCompare: " + zerosCompare + " " + zerosCompare2);
    }

    private static void testArithmetic() {
        try {
            BigDecimal value = new BigDecimal(1);
            value = value.divide(new BigDecimal(3));
            System.out.println(value);
        } catch (ArithmeticException e) {
            System.out.println("Failed to devide. " + e.getMessage());
        }
    }

    private static void testConstructors() {
        double doubleValue = 35.7;
        BigDecimal fromDouble = new BigDecimal(doubleValue);
        BigDecimal fromString = new BigDecimal("35.7");

        boolean decimalsEqual = fromDouble.equals(fromString);
        boolean decimalsEqual2 = fromString.equals(fromDouble);

        System.out.println("From double: " + fromDouble);
        System.out.println("decimalsEqual: " + decimalsEqual + " " + decimalsEqual2);
    }
}

It prints

From double: 35.7000000000000028421709430404007434844970703125
decimalsEqual: false false
zerosAreEqual: false false
zerosCompare: 0 0
Failed to devide. Non-terminating decimal expansion; no exact representable decimal result.

How about storing BigDecimal into a database? Hell, it also stores as a double value??? At least, if I use mongoDb without any advanced configuration it will store BigDecimal.TEN as 1E1.

Possible solutions?

I came with one - use String to store BigDecimal in Java as a String into the database. You have validation, for example @NotNull, @Min(10), etc... Then you can use a trigger on update or save to check if current string is a number you need. There are no triggers for mongo though. Is there a built-in way for Mongodb trigger function calls?

There is one drawback I am having fun around - BigDecimal as String in Swagger defenition

I need to generate swagger, so our front-end team understands that I pass them a number presented as a String. DateTime for example presented as a String.

There is another cool solution I read in the article above... Use long to store precise numbers.

A standard long value can store the current value of the Unites States national debt (as cents, not dollars) 6477 times without any overflow. Whats more: it’s an integer type, not a floating point. This makes it easier and accurate to work with, and a guaranteed behavior.


Update

https://stackoverflow.com/a/27978223/4587961

Maybe in the future MongoDb will add support for BigDecimal. https://jira.mongodb.org/browse/SERVER-1393 3.3.8 seems to have this done.

It is an example of the second approach. Use scaling. http://www.technology-ebay.de/the-teams/mobile-de/blog/mapping-bigdecimals-with-morphia-for-mongodb.html

1
  • 3
    Maybe a lot of the BigDecimal problems mentioned would go away if you use it with an MathContext with precision set to 2.
    – Salix alba
    Apr 15, 2021 at 14:00
0

There is an extensive example of how to do this on javapractices.com. See in particular the Money class, which is meant to make monetary calculations simpler than using BigDecimal directly.

The design of this Money class is intended to make expressions more natural. For example:

if ( amount.lt(hundred) ) {
 cost = amount.times(price); 
}

The WEB4J tool has a similar class, called Decimal, which is a bit more polished than the Money class.

0
NumberFormat.getNumberInstance(java.util.Locale.US).format(num);
2
  • I like the simplicity of this answer but it will return 1.5 instead of 1.50 dropping the final zero.
    – JesseBoyd
    Aug 22, 2017 at 20:40
  • Try: NumberFormat.getCurrencyInstance(java.util.Locale.US).format(num); instead.
    – SedJ601
    Apr 18, 2018 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.