This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes double decimal problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double decimal problems" Watch "double decimal problems" New topic
Author

double decimal problems

Bredan Tomong
Greenhorn

Joined: Dec 21, 2000
Posts: 13
Try follow coding. Guess what's the output?
int size = 55;
double total = 0.0;
for (int i = 0; i < size; i++) {
total += 0.1 + i;
}
System.out.println("Total is : " + total);
The answer is "Total is : 1490.4999999999995"
Any ideas about fixing the problem except using BigDecimal?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I have to assume that the problem is the rounding issue. doubles and floats both have decimals. In order to be precise with decimals you would need to have an infinite number of decimal places (of course impossible) therefore they are ALWAYS not quite exact.
BigDecimal is probably your best bet.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Hey, I JUST saw this article in JavaWorld
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
So I thought that I would share it with you. How timely.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double decimal problems
 
Similar Threads
rms size
Counting in Array List/ Standard deviation
output prints null
calculate amount due
MemoryImageSource and byte arrays