Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It would've been <code>RoundingMode.HALF_EVEN</code> in Java; it's <code>RoundingMode::HALF_EVEN</code> in Ruby. You may also be able to use the <code>int</code> constants overload (i.e. <a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html#ROUND_HALF_EVEN" rel="nofollow noreferrer"><code>BigDecimal::ROUND_HALF_EVEN</code></a>), but the <code>enum</code> overload is definitely the way to go.</p> <p>You can control the <em>scale</em> of the quotient using the <a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html#divide%28java.math.BigDecimal,%20int,%20java.math.RoundingMode%29" rel="nofollow noreferrer"><code>divide(BigDecimal divisor, int scale, RoundingMode mode)</code></a> overload.</p> <p>Here's a Java snippet:</p> <pre><code> BigDecimal one = BigDecimal.ONE; BigDecimal three = BigDecimal.valueOf(3); System.out.println(one.divide(three, 10, RoundingMode.DOWN)); // prints "0.3333333333" System.out.println(one.divide(three, 10, RoundingMode.UP)); // prints "0.3333333334" System.out.println(one.divide(three, 333, RoundingMode.UNNECESSARY)); // throws java.lang.ArithmeticException: Rounding necessary </code></pre> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2749375/arithmeticexception-thrown-during-bigdecimal-divide"><code>ArithmeticException</code> thrown during <code>BigDecimal.divide</code></a> <ul> <li><code>BigDecimal</code> can not represent <code>1/3</code> exactly (because it has a non-terminating decimal expansion)</li> </ul></li> </ul> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/math/RoundingMode.html" rel="nofollow noreferrer"><code>java.math.RoundingMode</code></a> </li> <li><a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html" rel="nofollow noreferrer"><code>java.math.BigDecimal</code></a> <blockquote> <p>A <code>BigDecimal</code> consists of an arbitrary precision integer unscaled value and a 32-bit integer <em>scale</em>. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. </p> </blockquote></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload