File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes java decimal multiplication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java decimal multiplication" Watch "java decimal multiplication" New topic
Author

java decimal multiplication

Cindy Jones
Ranch Hand

Joined: Oct 08, 2002
Posts: 66
Hi,

35.87 * 100 = 3586.9999999999995
not 3587.
1. Why is this?
2. Most other decimal numbers work fine when I use this logic to get the same number as the original number without decimals (after I have removed the part after the decimal point)

Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Because data types like float and double do not have infinite precision. This does not happen in only in Java, it happens in almost every progamming language that exists.

Have a look at Wikipedia: Floating point. Here's a very technical explanation: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

If you need to write software that deals with for example financial data (money), you shouldn't use float or double. You should use class BigDecimal, which can store an arbitrary number of decimal digits.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java decimal multiplication