Class DoubleVsBigDecimal

java.lang.Object
org.gbif.utils.number.DoubleVsBigDecimal

public class DoubleVsBigDecimal extends Object
Set of tests to illustrate the issue with double and how some problems can be solved with BigDecimal. BigDecimal is specially useful to work with base 10 type of data (money, metric measures). Be aware that BigDecimal requires more memory to work with (it's an Object) and it's also slower on computations (it's an immutable Object).
  • Constructor Details

  • Method Details

    • testOneTenthAdditions

      @Test public void testOneTenthAdditions()
      Demonstrates that the sum of 1/10 (10 times) is not exactly equals to 1d using double.
    • testDoubleApproximation

      @Test public void testDoubleApproximation()
      Demonstrates the effect of Double approximations on equality.
    • testRoundingUsingFormat

      @Test public void testRoundingUsingFormat()
      Demonstrates the possible issue with the default rounding mode of NumberFormat (RoundingMode.HALF_EVEN). This is a modified version from the blog post: https://blogs.oracle.com/CoreJavaTechTips/entry/the_need_for_bigdecimal No idea why the blog says "why does 90.045 round down to 90.04 and not up to 90.05" because it is not the case.
    • testAdditionAssociativity

      @Test public void testAdditionAssociativity()
      Example from https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems Simply demonstrates that (a + b) + c is not necessarily equal to a + (b + c) using double.