Jan 31

Use the compareTo() method instead of the equals() method.
equals() only considers two BigDecimal’s to be equal if they have same value and scale (eg. 1.25 is not equal to 1.250 when using equals() method).

BigDecimal a = new BigDecimal("1.25");
BigDecimal b = new BigDecimal("1.250");
if (a.compareTo(b)==0)
{
   System.out.println("They are equal");
}

written by objects \\ tags: , ,

Oct 01

That’s a job for the BigDecimal class


public static String formatToSignificant(double value,
   int significant)
{
   MathContext mathContext = new MathContext(significant,
      RoundingMode.DOWN);
   BigDecimal bigDecimal = new BigDecimal(value,
      mathContext);
   return bigDecimal.toPlainString();
} 

written by objects \\ tags: ,