Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the proper syntax for Java's BigDecimal?
    text
    copied!<p>I have two <code>double</code> counters that I'm incrementing based on certain criteria. The counters will increment by either 1 or .01, and the results summed together for a serial number of <code>1.00</code>, <code>1.01</code>, <code>1.02</code>, etc. I'm getting results like <code>1.01999999999</code> or <code>1.111111111111</code>, an expected condition due to the nature of <code>double</code>.</p> <p>I am trying to use BigDecimal, and although the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html" rel="nofollow">Java Documentation</a> indicates</p> <pre><code>BigDecimal(double val) Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. </code></pre> <p>I cannot get the code to work. </p> <pre><code> .... if (condition1) { indexNo = indexNo + 1; subIndexNo = .00; } else if (condition2) { subIndexNo = subIndexNo + .01; } recNo = indexNo + subIndexNo; BigDecimal record = BigDecimal(double recNo); </code></pre> <p>Error messages from the compiler</p> <pre><code>C:\pathtojava\sync\java\bin&gt;javac ParseWhiData.java ParseWhiData.java:97: error: '.class' expected BigDecimal record = BigDecimal(double recNo); ^ ParseWhiData.java:97: error: ';' expected BigDecimal record = BigDecimal(double recNo); ^ 2 errors </code></pre> <p>What am I doing wrong while accessing BigDecimal? What's the proper syntax to convert a double to a Big Demimal, and, most importantly, will it turn <code>1.019999999999</code> to <code>1.02</code> </p>
 

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