Don't Use double Constructor on BigDecimal

August 1, 2013

Avoid using the BigDecimal constructor in Java that takes a double:

public class Test
{
  public static void main(String[] argv)
  {
    double lDouble = 123.45;
    System.out.println(lDouble);
    BigDecimal lDoubleBigDecimal = new BigDecimal(lDouble);
    System.out.println(lDoubleBigDecimal);
    BigDecimal lStringBigDecimal = new BigDecimal(Double.toString(lDouble));
    System.out.println(lStringBigDecimal);
  }
}

Running this program gave me the following output:

123.45
123.4500000000000028421709430404007434844970703125
123.45

References