Multiplying two numbers using BigDecimal returns a wrong value

You’re not multiplying two numbers using BigDecimal. You’re multiplying them using double arithmetic, and passing the result to the BigDecimal constructor.

You want:

new BigDecimal("0.06").multiply(new BigDecimal("3")).toString()

Note that you do want the values in strings – otherwise you’re using the double value for 0.06, which isn’t exactly 0.06… you’ve lost information before you start. (You don’t really need the string form of 3, but I’ve done so for consistency.)

For example:

System.out.println(new BigDecimal(0.06));

prints

0.059999999999999997779553950749686919152736663818359375

Leave a Comment

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