wood burning stoves 2.0*
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
Author

Double multiplication problem

Prasanna Kulkarni
Greenhorn

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

double amount = 128.98;
System.out.println(amount*100);

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
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
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: 757
Which data type should we use for arithmetic option of currency ?


Jigar Naik


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 13, 2005
Posts: 38873
    
  23
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: http://aspose.com/file-tools
 
subject: Double multiplication problem