adding 2 BigDecimal values [duplicate]

BigDecimal is immutable. Every operation returns a new instance containing the result of the operation: BigDecimal sum = x.add(y); If you want x to change, you thus have to do x = x.add(y); Reading the javadoc really helps understanding how a class and its methods work.

Round up BigDecimal to Integer value

setScale returns a new BigDecimal with the result, it doesn’t change the instance you call it on. So assign the return value back to value: value = value.setScale(0, RoundingMode.UP); Live Example I also changed it to RoundingMode.UP because you said you always wanted to round up. But depending on your needs, you might want RoundingMode.CEILING … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)