File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Double multiplication problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Double multiplication problem" Watch "Double multiplication problem" New topic

Double multiplication problem

Prasanna Kulkarni

Joined: Jan 07, 2004
Posts: 15
Hi, this is something funny

double amount = 128.98;

This gives answer - 12897.999999999998 and not 12898.00 !

This only happens after 128.98 [E.g for 128.98, 129.98 etc] - also specifically for 0.98, not even 0.99...

Anybody knows the reason ?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
More of a beginner's question. That sort of thing happens all the time, when you least expect it, and that's why you don't use double arithmetic for money.

We have a FAQ (no 20) with useful links.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 761
Which data type should we use for arithmetic option of currency ?

Jigar Naik

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

BigDecimal, or maybe BigInteger depending on how your currency works.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
You can get away with integer arithmetic if you declare your money in cents/pence/etc. This thread shows an example of the use of BigDecimal.
I agree. Here's the link:
subject: Double multiplication problem
It's not a secret anymore!