Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have mentioned, you'll probably want to use the <a href="https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html" rel="noreferrer"><code>BigDecimal</code></a> class, if you want to have an exact representation of 11.4.</p> <p>Now, a little explanation into why this is happening:</p> <p>The <code>float</code> and <code>double</code> primitive types in Java are <a href="http://en.wikipedia.org/wiki/Floating_point" rel="noreferrer">floating point</a> numbers, where the number is stored as a binary representation of a fraction and a exponent.</p> <p>More specifically, a double-precision floating point value such as the <code>double</code> type is a 64-bit value, where:</p> <ul> <li>1 bit denotes the sign (positive or negative).</li> <li>11 bits for the exponent.</li> <li>52 bits for the significant digits (the fractional part as a binary).</li> </ul> <p>These parts are combined to produce a <code>double</code> representation of a value.</p> <p>(Source: <a href="http://en.wikipedia.org/wiki/Double_precision" rel="noreferrer">Wikipedia: Double precision</a>)</p> <p>For a detailed description of how floating point values are handled in Java, see the <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.3" rel="noreferrer">Section 4.2.3: Floating-Point Types, Formats, and Values</a> of the Java Language Specification.</p> <p>The <code>byte</code>, <code>char</code>, <code>int</code>, <code>long</code> types are <a href="http://en.wikipedia.org/wiki/Fixed-point_arithmetic" rel="noreferrer">fixed-point</a> numbers, which are exact representions of numbers. Unlike fixed point numbers, floating point numbers will some times (safe to assume "most of the time") not be able to return an exact representation of a number. This is the reason why you end up with <code>11.399999999999</code> as the result of <code>5.6 + 5.8</code>.</p> <p>When requiring a value that is exact, such as 1.5 or 150.1005, you'll want to use one of the fixed-point types, which will be able to represent the number exactly.</p> <p>As has been mentioned several times already, Java has a <a href="https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html" rel="noreferrer"><code>BigDecimal</code></a> class which will handle very large numbers and very small numbers. </p> <p>From the Java API Reference for the <code>BigDecimal</code> class:</p> <blockquote> <p>Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. 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. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10^-scale).</p> </blockquote> <p>There has been many questions on Stack Overflow relating to the matter of floating point numbers and its precision. Here is a list of related questions that may be of interest:</p> <ul> <li><a href="https://stackoverflow.com/questions/177506/why-do-i-see-a-double-variable-initialized-to-some-value-like-214-as-2139999961">Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?</a></li> <li><a href="https://stackoverflow.com/questions/240876/print-really-big-numbers">How to print really big numbers in C++</a></li> <li><a href="https://stackoverflow.com/questions/56947/how-is-floating-point-stored-when-does-it-matter">How is floating point stored? When does it matter?</a></li> <li><a href="https://stackoverflow.com/questions/61872/use-float-or-decimal-for-accounting-application-dollar-amount">Use Float or Decimal for Accounting Application Dollar Amount?</a></li> </ul> <p>If you really want to get down to the nitty gritty details of floating point numbers, take a look at <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" rel="noreferrer">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    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