• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Unexpected value when multiplying a double

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

We are converting a monetary value (say dollars and cents) which is stored in a Double into an integer which stores the number in cents by multiplying the value by 100. I would have expected this to work without a problem but I recently discovered that we do not always get the value we expected e.g. if a double contains the value 2.07 and is multiplied by 100 the result is not 207.000 but 206.99999999999997
But if 2.18 is multiplied by 100 the result is 218.00000000000003.

This can be seen by executing the following code




Im sure that this is some feature of the IEEE floating point standard that I dont fully understand but what now concerns me is how I can fix this problem so that the cent value is always correct. Would it be enough to round my double value up before converting it to an int as follows



All comments appreciated.
John
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Computers simply cannot represent any arbitrary floating point number with infinite precision. Use java.text.DecimalFormat to print numbers on screen with a specified number of digits.

Read this if you want to know all the technical details:
What Every Computer Scientist Should Know About Floating-Point Arithmetic
[ August 24, 2005: Message edited by: Jesper de Jong ]
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Double implementation actually has a "bug", because not all numbers can be represented in a binary form.

http://forum.java.sun.com/thread.jspa?threadID=386078&messageID=1660010
http://forum.java.sun.com/thread.jspa?threadID=618476&start=0&tstart=0

At the company I work for (a bank) we use the java.math.BigDecimal to handle currency. It is slighly better at it.
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm... what he said. Never leave a post editor open over lunch =)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic