Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that 600 and 500 are integer values, and a division between them will have an integer result, which you assign to a long integer. After this, the fractional part of the result is gone (twice) and you can't get it back.</p> <p>In order to get a fractional result, at least one of the input values of the division needs to be non-integer. You can achieve this by casting them to <code>double</code> or <code>float</code>, or by making the literals explicitly double or float like this:</p> <pre><code>500.0 // implicitly double 500f // float literal 500d // double literal </code></pre> <p>However, printing the result of a double/float divison will often have more decimal places than you want even if they shouldn't be in the result (e.g. <code>1/10</code> can result in <code>0.100000001490116119384765625</code>), due to <a href="http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" rel="nofollow noreferrer">the way these numbers are stored as binary fractions</a>.</p> <p>You can eliminate these unwanted digits by using <code>java.lang.DecimalFormat</code>, but <code>BigDecimal</code> is a better option, since it internally uses decimal fractions and will therefore deliver the actually correct results while offering exact control over rounding modes. Of course, this requires you to use <code>BigDecimal</code> for the calculation itself, rather than as "voodoo ingredient" to fix an already broken result.</p> <pre><code>new BigDecimal(600).divide(new BigDecimal(500)) </code></pre> <p>Read the <a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html" rel="nofollow noreferrer">API documentation for <code>BigDecimal</code></a> for more information.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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