{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "@id": "https://www.vidyasource.com/blog/zero-tolerance-java/",
  "url": "https://www.vidyasource.com/blog/zero-tolerance-java/",
  "mainEntityOfPage": "https://www.vidyasource.com/blog/zero-tolerance-java/",
  "headline": "Zero Tolerance: Checking a Java BigDecimal for Zero",
  "description": "Is your Java BigDecimal zero? Compare signum, compareTo, and equals to find the check that works.",
  "datePublished": "2014-11-06T00:00:00.000Z",
  "author": {
    "@type": "Person",
    "name": "Neil Chaudhuri",
    "jobTitle": "President",
    "url": "https://www.linkedin.com/in/neil-chaudhuri/",
    "sameAs": "https://www.linkedin.com/in/neil-chaudhuri/"
  },
  "publisher": {
    "@id": "https://www.vidyasource.com/#organization"
  },
  "image": "https://www.vidyasource.com/img/blog/zero.jpg",
  "keywords": [
    "Java",
    "Programming",
    "Testing"
  ],
  "articleBody": "Experienced Java developers know that Java 5 brought many significant improvements to the language.\nOne of them was the introduction of [BigDecimal](http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html), an\nabstraction for arbitrary-precision decimal numbers that gives you arithmetic functionality and complete control over\nrounding and scale. All of this flexibility and power made `BigDecimal` the *de facto* class Java developers used to model money--at\nleast until Java 7 gave us the [Currency](http://docs.oracle.com/javase/8/docs/api/java/util/Currency.html) class.\nOn a recent project though with an API that doesn’t use `Currency`, I had a simple question:\n\n*How do you test if a BigDecimal is 0?*\n\nIt turns out that the power of `BigDecimal` makes this question a lot trickier than you might think.\n\nMy first instinct was to do what you are always supposed to do with equality of objects--use the\n[equals](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) method. So I used\nBigDecimal’s [ZERO](http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#ZERO) constant and did this.\n\n~~~java\nif (price.equals(BigDecimal.ZERO) {\n   //Do stuff\n}\n~~~\n\nWe do this kind of thing all the time without a second thought. The problem was that my unit test failed, and it turns\nout it was because the condition always evaluated to `false`--even when `price` was definitely 0.\n\nIt turns out zero isn’t always zero. At least not when it comes to `BigDecimal`.\n\nThe answer is in `BigDecimal`’s implementation of the [equals](http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#equals-java.lang.Object-)\nmethod.\n\n> Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).\n\nI assumed `equals` just compared values, but scale matters. `BigDecimal.ZERO` has scale of 0, but the zero value in `price` in my test did not.\nThat’s why things went wrong.\n\nWhat’s the right way to check if your BigDecimal instance is 0? It turns out there are two right ways.\n\nOne of them makes perfect sense if you know [Comparable](http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html).\n(If you don’t, check out our [tutorial](https://www.vidyasource.com/tutorials/comparison-shopping/) on the topic.)\n\n~~~java \nif (price.compareTo(BigDecimal.ZERO) == 0) {\n   //Do stuff\n}\n~~~\n\nThis approach is intuitive and readable.\n\nIf you want to sacrifice readability to gain a few milliseconds of performance and earn some cuteness points, you can do this.\n\n~~~java \nif (price.signum() == 0) {\n   //Do stuff\n}\n~~~\n\nBigDecimal, like all values in Java, represents a signed number, and the [signum](http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#signum--)\nmethod returns the [sign](http://en.wikipedia.org/wiki/Sign_function) of the underlying number. Technically, `signum` doesn’t\ntest the numerical value, but since a BigDecimal representing 0 always has a signum of 0...it works out the same in your condition.\n\nThe next time you are checking if your BigDecimal is 0, just remember. [It depends on what the meaning of the word of is is](https://www.youtube.com/watch?v=j4XT-l-_3y0)."
}