Contents
Why is BigDecimal better than double for representing dollar amounts?
A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001 ) could result in the 0.001 being dropped alltogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen.
Is BigDecimal good for MONEY?
Float and double are bad for financial (even for military use) world, never use them for monetary calculations. If precision is one of your requirements, use BigDecimal instead. All floating point values that can represent a currency amount (in dollars and cents) cannot be stored exactly as it is in the memory.
Why is BigDecimal the best data type for currency?
It all comes down to precision, and Java’s BigDecimal seems the correct answer on that platform because it gives you the best support for specifying and preserving what can be highly variable precision.
How is The BigDecimal used in Java stack?
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 You should have no difficulties with the $ and precision.
When to use a string in a BigDecimal?
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. Primitive numeric types are useful for storing single values in memory.
When to use BigDecimal to round up money?
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.