Okay, making a game trying to use a floating variable Money# to handle cash.
My problem is displaying the variable correctly when the value of this variable is an even dime or dollar (such as $10.9 for $10.90 or $10 for $10.00)
Right now I am using the following lines of text to place one or two additional zeros of text to compensate:
if money#=int(money#) then center text 545,230,"$"+str$(money#)+".00":goto gamer
if int(money#*10)=money#*10 then center text 545,230,"$"+str$(money#)+"0":goto gamer
center text 545,230,"$"+str$(money#)
If i set the money# variable to $100 at the beginning of the program, that code works correctly.
But if I subtract another money variable, say food# which I set to 1.99 (food#=1.99) and I use the same lines of code, after it is subtracted ten times.. or twenty or any other number of times which my above code should fix, it doesn't fix it. For example 100-1.99 ten times would be 80.1 which my above code SHOULD fix to add an extra "0" to to make it display 80.10.
So to figure out what's going on, I wrote a quick program to display to the screen what's going on here:
cls
r#=100:f#=1.99
text 310,100,str$(r#):text 400,100,str$(r#*10):text 500,100,str$(int(r#*10))
if r#*10=int(r#*10) then text 450,120,"YES":goto fick
text 450,120,"NOPE"
fick:
for x=1 to 10:dec r#,f#:next x
text 310,200,str$(r#):text 400,200,str$(r#*10):text 500,200,str$(int(r#*10))
if (r#*10)=(int(r#*10)) then text 450,220,"YES":goto fack
text 450,220,"NOPE"
fack:
do
loop
from that program, it's obvious that when I set a floating variable at a certain value directly, it sees that r#*10=int(r#*10), but after reducing that variable, even though I display both as being exactly the same value, the program doesn't see them as the same...
My question is WHY and what is the best way to resolve this problem??
Thank you!
- Zaxx
OKAY UPDATE with a POTENTIAL SOLUTION, but I don't understand why this works or the above code doesn't work (Code Insert #2)
Here is my work around:
cls
r#=100:f=199
text 310,100,str$(r#):text 400,100,str$(r#*10):text 500,100,str$(int(r#*10))
if r#*10=int(r#*10) then text 450,120,"YES":goto fick
text 450,120,"NOPE"
fick:
r#=r#*100
for x=1 to 10:dec r#,f:next x
r#=r#/100
text 310,200,str$(r#):text 400,200,str$(r#*10):text 500,200,str$(int(r#*10))
if (r#*10)=(int(r#*10)) then text 450,220,"YES":goto fack
text 450,220,"NOPE"
fack:
do
loop
Notice the changes I made was to make F# a regular variable instead of "F=199" vs the old "F#=1.99"
- Then I simply multiplied my Money# floating variable by 100 before subtracting F ten times in the for/next loop, followed by dividing my money# variable by 100 after the for/next loop.
Why this works and why the code insert #2 above doesn't is beyond me.. I would love an explanation and any advice on better code suggestions if you have them.
Thanks again!
- Zaxx