There are many monetary values calculation in the financial or e-commerce application, and there is one question that arises for this – Should we use double
or float
data type to represent the monetary values?
Answer: Always uses java.math.BigDecimal
to represent the monetary values.
1. Double or Float?
Here is an example of using double
and float
to represent the monetary values in Java.
JavaMoney.java
package com.favtuts.classic; import java.text.DecimalFormat; public class JavaMoney { private static DecimalFormat df = new DecimalFormat("0.00"); public static void main(String[] args) { representMonetaryValuesWithDoubleAndFloat(); } static void representMonetaryValuesWithDoubleAndFloat() { 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); System.out.println("--------------------"); double i = 2.0; double j = 1.7; System.out.println("double : " + (i - j)); System.out.println("double : " + df.format(i - j)); float i2 = 2.0f; float j2 = 1.7f; System.out.println("float : " + (i2 - j2)); System.out.println("float : " + df.format(i2 - j2)); } }
Output – It cannot calculate all the decimals precisely.
0.8999999999999999
0.8
0.7
0.6000000000000001
0.5
0.3999999999999999
0.30000000000000004
0.19999999999999996
0.10000000000000009
0.0
--------------------
double : 0.30000000000000004
double : 0.30
float : 0.29999995
float : 0.30
2. BigDecimal
To avoid the decimal issue above, we can use BigDecimal
to represent the monetary values; furthermore, we can control the BigDecimal
scale much more straightforward.
JavaMoney2.java
package com.favtuts.classic; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class JavaMoney { private static DecimalFormat df = new DecimalFormat("0.00"); public static void main(String[] args) { representMonetaryValuesWithBigDecimal(); } static void representMonetaryValuesWithBigDecimal() { System.out.println("--- BigDecimal-----"); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.1))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.2))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.3))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.4))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.5))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.6))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.7))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.8))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(1.9))); System.out.println(BigDecimal.valueOf(2.00).subtract(BigDecimal.valueOf(2))); System.out.println("--------------------"); BigDecimal i = BigDecimal.valueOf(2.0); BigDecimal j = BigDecimal.valueOf(1.7); System.out.println("double : " + i.subtract(j)); System.out.println("double : " + i.subtract(j).setScale(5, RoundingMode.HALF_UP)); } }
Output – BigDecimal
performs exact decimal arithmetic.
--- BigDecimal-----
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0.0
--------------------
double : 0.3
double : 0.30000
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-basic/classic