| | |
odd numbers
![]() |
•
•
Join Date: Oct 2007
Posts: 60
Reputation:
Solved Threads: 0
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.
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.
Please post your code
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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.
•
•
•
•
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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
![]() |
Similar Threads
- Loop counting odd and even numbers (C++)
- add even (or odd) numbers from input (C++)
- Odd numbers in C++? (C++)
- Adding the sum of odd numbers. Please help (Visual Basic 4 / 5 / 6)
- print odd and even numbers (Assembly)
- Odd And Even (Java)
Other Threads in the Java Forum
- Previous Thread: need help with the GUI
- Next Thread: Java mock exams
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






