How to calculate monetary values in Java

Monetary values are usually programmed in the financial or e-commerce application. When you deal with “money” value, it’s always come to a question – Should I use double or float data type to represent the monetary values?

Answer : Always uses java.math.BigDecimal to represent the monetary values.

1. Monetary values – Double

Here is an example to use double to represent the monetary values in Java.

MoneyDouble.java

package com.mkyong.test;

public class MoneyDouble {

    public static void main(String[] argv) {

        System.out.println("--- Normal Print-----");
        System.out.println(2.00 - 1.1);
        System.out.println(2.00 - 1.2);
        System.out.println(2.00 - 1.3);
        System.out.println(2.00 - 1.4);
        System.out.println(2.00 - 1.5);
        System.out.println(2.00 - 1.6);
        System.out.println(2.00 - 1.7);
        System.out.println(2.00 - 1.8);
        System.out.println(2.00 - 1.9);
        System.out.println(2.00 - 2);
    }

}

Output – It cannot calculate all double decimals precisely.


--- Normal Print-----
0.8999999999999999
0.8
0.7
0.6000000000000001
0.5
0.3999999999999999
0.30000000000000004
0.19999999999999996
0.10000000000000009
0.0

2. Monetary values – BigDecimal

To avoid the decimal issue above, you can use BigDecimal to represent the monetary values.

MoneyBigDecimal.java

package com.mkyong.test;

import java.math.BigDecimal;

public class MoneyBigDecimal {

    public static void main(String[] argv) {

        System.out.println("--- BigDecimal-----");
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.1")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.2")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.3")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.4")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.5")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.6")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.7")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.8")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.9")));
        System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("2")));
    }

}

Output – BigDecimal performs exact decimal arithmetic.


--- BigDecimal-----
0.90
0.80
0.70
0.60
0.50
0.40
0.30
0.20
0.10
0.00
Note
BigDecimal calculations are slower than those with primitive type calculations, which may be an issue for the mission critical calculation application. For most e-commerce applications, it should be fine.

References

  1. BigDecimal JavaDoc
  2. How to Use Java BigDecimal

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
prashant
Guest
prashant

how java decides the double precision
ex:
10.2*2 = 20.4
10.3*3=30.599999999999998
why the result is different in both cases?

subes
Guest
subes

See https://github.com/subes/invesdwin-util#decimal for an alternative to working with Double directly by using a fluent API around it that is very fast since being designed for financial strategy backtesting.

Prateek Ashtikar
Guest
Prateek Ashtikar

Very useful. I was struggling to do precisely calculations. Thanks!!