944,033 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1572
  • Java RSS
Oct 30th, 2007
0

odd numbers

Expand Post »
Hi I am making an vending machine application. Now the vending Machine takes 5, 10, 20, 50 pence and 1 pound coins. Now I create a String object and use this to read input from the user i.e. read the denomination (Money e.g. 5 or 10 pence) the user enters. Now I parse the string to an double. The user selects an item from the machine which then asks the user to enter coins to complete the purchase. When the user adds 5 pence coins for an item costing 45 pence, a loop is activated to repeat the method in which the coins are to be inserted until the amount entered = or is greater than the price of the snack selected. By the time it gets to 35 pence everything is alright however after adding another 5 pence the number becomes strange

Here is a copy of what happens


Insert Coins : - .05
0.05

Insert Coins : - .05
0.1

Insert Coins : - .05
0.15000000000000002

Insert Coins : - .05
0.2

Insert Coins : - .05
0.25

Insert Coins : - .05
0.3

Insert Coins : - .05
0.35

Insert Coins : - .05
0.39999999999999997

Insert Coins : - .05
0.44999999999999996

Insert Coins : - .05
0.49999999999999994

As such it requires me to enter more money than is necessary.
May you help explain this and possible suggest a solution.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
Grub is offline Offline
60 posts
since Oct 2007
Oct 30th, 2007
0

Re: odd numbers

Please post your code
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Oct 30th, 2007
0

Re: odd numbers

This is natural behavior for floating point numbers. They are a limited precision representation of a decimal value and you should not expect them to be an exact result. This is why they should not be used for financial calculations which require perfect precision.
Last edited by Ezzaral; Oct 30th, 2007 at 1:04 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 30th, 2007
0

Re: odd numbers

You should stick to integers and work with them in terms of cents. So 1 = 1 cent rather than 1 dollar in your example. Calculations could also be unnoticably faster as well.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Oct 31st, 2007
0

Re: odd numbers

Click to Expand / Collapse  Quote originally posted by Phaelax ...
You should stick to integers and work with them in terms of cents. So 1 = 1 cent rather than 1 dollar in your example. Calculations could also be unnoticably faster as well.

That is a good idea. Floating points normally behave like that, you have explanation on Ezzarals' last post.

Basically used in one format, either in cents or dolors.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: need help with the GUI
Next Thread in Java Forum Timeline: Java mock exams





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC