title: Java's Bigdecimal.divide and rounding
body: <p>At work, we found a problem when trying to divide a large number by 1000. This number came from the database.</p>
  
  <p>Say I have this method:</p>
  
  <pre><code>private static BigDecimal divideBy1000(BigDecimal dividendo) {
      if (dividendo == null) return null;
  
      return dividendo.divide(BigDecimal.valueOf(1000), RoundingMode.HALF_UP);
  }
  </code></pre>
  
  <p>When I make the following call</p>
  
  <pre><code>divideBy1000(new BigDecimal("176100000"))
  </code></pre>
  
  <p>I receive the expected value of 176100. But if I try the line below</p>
  
  <pre><code>divideBy1000(new BigDecimal("1761e+5"))
  </code></pre>
  
  <p>I receive the value 200000. Why this occurs? Both numbers are the same with different representation and the latest is what I receive from database. I understand that, somehow, the JVM is dividing the number 1761 by 1000, rounding up and filling with 0's at the end.</p>
  
  <p>What is the best way to avoid this kind of behavior? Keep in mind that the original number is not controlled by me.</p>