What type would you map BigDecimal in Java/Hibernate in MySQL?

DECIMAL and NUMERIC. The recommended Java mapping for the DECIMAL and NUMERIC types is java.math.BigDecimal. The java.math.BigDecimal type provides math operations to allow BigDecimal types to be added, subtracted, multiplied, and divided with other BigDecimal types, with integer types, and with floating point types. The method recommended for retrieving DECIMAL and NUMERIC values is ResultSet.getBigDecimal. … Read more

Why does toString fail to produce the correct value on an immutable BigDecimal?

It’s not so hard to track down the reason for the odd behavior. The divide call goes to public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) { return divide(divisor, scale, roundingMode.oldMode); } This, internally, delegates to another divide method, based on the rounding mode: public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) { if (roundingMode … Read more

BigDecimal.ZERO vs. new BigDecimal(0). Which to use and why?

Mathematically, they’re the same. Plus, since BigDecimals are immutable, you don’t need to worry about creating new instances to do new calculations. As soon as you perform some operation on your totalCurrentSales instance, you’ll actually be creating a new BigDecimal and reassigning the totalCurrentSales reference to the new value. From a instantiation perspective, they’re not … Read more

Which PostgreSQL column type should be used to store a Java BigDecimal?

See PostgreSQL datatypes – perhaps Numeric, which can act as an arbitrary precision type (this is a PostgreSQL extension). …without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision. I am not entirely sure what “implementation limit on … Read more

MongoDB – What about Decimal type of value?

If you want an exact representation for financial purposes, then doubles or floating point values are unsuitable as the fractional parts are subject to rounding error. Certain decimal values cannot not be represented using binary-based floating points and must be approximated. For a less technical intro, see The trouble with rounding floating point numbers; if … Read more

Equals operator for zeros (BigDecimal / Double) in Java

BigDecimal ‘equals’ compares the value and the scale. If you only want to compare values (0 == 0.0) you should use compareTo: BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0.0)) == 0 //true BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0)) == 0 //true See the javadoc. As for the Double comparison, as explained by other answers, you are comparing a Double with an Integer in new Double(0.0).equals(0), which … Read more

Best way to convert Locale specific String to BigDecimal

It seems like there is no other way since java.Lang.Number doesn’t have a method which returns a BigDecimal type. Anyway it makes sense because BigDecimal only accepts strings which are properly formatted not like “2.105,88” but like “2105.88”. Let me show your my code: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; public … Read more