In this post we will talk and learn Which data type would you choose for storing currency values like Trading Price ? What is your opinion about Float, Double and BigDecimal data types ?
Let’s see how double data type works for financial calculation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials.client; /** * @author KK JavaTutorials * Float & Double data types are Bad for financial world, never use them for monetary * calculations. */ public class DoubleForCurrencyTest_1 { public static void main(String[] args) { double total = 0.2; for (int i = 0; i < 100; i++) { total += 0.2; } System.out.println("Total Amount = " + total); } } |
Sample output of above program:
Total Amount = 20.19999999999996
The output suppose to be 20.20 (20 dollars and 20 cents), but because of floating point calculation made it 20.1999999999..
Let’s see now how BigDecimal data type works for financial calculation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials.client; import java.math.BigDecimal; /** * @author KK JavaTutorials * BigDecimal or BigInteger is appropriate for financial calculations */ public class DoubleForCurrencyTest_2 { public static void main(String[] args) { double amt = 0.2; BigDecimal totalAmt = new BigDecimal(Double.toString(amt)); for (int i = 0; i < 100; i++) { totalAmt = totalAmt.add(new BigDecimal(Double.toString(amt))); } System.out.println("Total Amount = " + totalAmt); } } |
Sample output::
Total Amount = 20.2
How we format BigDecimal Value without getting exponentiation in the result & Strip the trailing zeros?
We may get exponentiation in the calculation result if we do not follow some best practices while using Bigdecimal data type. Below example is the code snippet which shows a good usage of handling the calculation result using Bigdecimal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kkjavatutorials.client; import java.math.BigDecimal; /** * @author KK JavaTutorials * How we can format BigDecimal Value without getting exponentiation in the result * & Strip the trailing zeros? */ public class DoubleForCurrencyTest_3 { public static void main(String[] args) { int scale = 4; //double value = 0.2222622; double value = 0.2000000; BigDecimal bigDecimal = new BigDecimal(Double.toString(value)); bigDecimal = bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_EVEN); System.out.println(bigDecimal); //Let's remove trailing Zeros and covert into string String bigDecimalinStr = bigDecimal.stripTrailingZeros().toPlainString(); System.out.println("Value = " + bigDecimalinStr); } } |
Sample Output:
0.2000
Value = 0.2
You may also like:
Why Java is not 100% Object-oriented language?
What are the differences between 32-bit and 64-bit versions of Java?
Can we call static method with null object?
Can we override static method in Java?
What will be the output of following java program?
What is the difference Between java.util.Date and java.sql.Date in Java ?
What is difference between using Serializable & Externalizable Interfaces in Java?
That’s all about Which data type would you choose for storing currency values like Trading Price in Java ?
If you have any feedback or suggestion please feel free to drop in blow comment box.