I get 5699.9999999 and i want 5700

intAantal[OptionNumber] = 114intTotaalAantal = 200double dblp = (double) intAantal[OptionNumber] / intTotaalAantal * 10000;This shoud get 5700 but he returns 5699.999999999
[200 byte] By [StreLochKia] at [2007-11-26 16:19:02]
# 1
Apply a [url= http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html]NumberFormat[/url] to the result.
corlettka at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...
# 2
use [url= http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html]BigDecimal[/url]. doubles can't be represented accurately in binary, hence this inaccuracy
georgemca at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...
# 3
> Apply a> [url= http://java.sun.com/j2se/1.4.2/docs/api/java/text> /NumberFormat.html]NumberFormat[/url] to the result.depends what he wanst the precision for. NumberFormat makes an imprecise number look precise, which may or may not be enough
georgemca at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...
# 4
Yeah, ooops, I didn't think about that... presumptious of me.
corlettka at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks!I used a float that fixt the problem
StreLochKia at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...
# 6
> I used a float that fixt the problemDid you even read the suggestions given to you?
CaptainMorgan08a at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...
# 7

> > I used a float that fixt the problem

>

> Did you even read the suggestions given to you?

He has no need to do that: the prob has been sol...

Edit:

@StreLochKi:

A float also suffers from the same inaccuracy than a double. Since a float is less precise as a double, it only looks like your problem has been solved.

Anyway, try reading this document to understand the limitations of floating point arithmetic:

http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2

So, as already suggested: either use a formatter like DecimalFormat to display your number upto a certain precision, or use BigDecimal for arbitrary precision arithmetic.

Good luck.

prometheuzza at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...
# 8

incidentally, OP, if you do decide to use BigDecimal, use the constructor that takes a String argument. this is the only way to guarantee the precision you want. eg

BigDecimal bd = new BigDecimal("100.0000000000");

bd is now precise to ten decimal places. using doubles or other primitives will make your BigDecimal adopt the precision of that primitive number

georgemca at 2007-7-8 22:42:22 > top of Java-index,Java Essentials,New To Java...