How BigDecimal is implemented?

The unscaled value of the BigDecimal is stored in a BigInteger. The precision and scale are stored separately in integer fields: BigInteger intVal int scale int precision BigInteger stores the integer as a big-endian array of 32 bit integers, and the sign separately as another 32-bit integer. int signum int[] mag But as Muhd says, … Read more

Java’s Bigdecimal.divide and rounding

As specified in javadoc, a BigDecimal is defined by an integer value and a scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10^(-scale)). So BigDecimal(“1761e+5”) has scale -5 and BigDecimal(176100000) has scale 0. The division of the two BigDecimal is done using the -5 and 0 scales respectively because … Read more

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 … Read more

What causes “Non-terminating decimal expansion” exception from BigDecimal.divide? [duplicate]

Non-terminating decimal need rounding When using divide you should use a MathContext with RoundingMode in case the exact result has an infinite number of decimals. Such is your case: MathContext mc = new MathContext(2, RoundingMode.HALF_UP) ; BigDecimal bd3 = bd1.divide(bd2, mc); Alternatively call divide with a rounding mode to use the scale of the numerator … Read more

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