decimals & counting change

Get help using Construct 2

Post » Mon Mar 09, 2015 8:10 pm

Hi everyone...

I was wondering if someone could point me to a thread with a tutorial or example of a decimal system formula that will add variables together and place the decimals in the right place for counting dollars?

say I add four vales of 25,
instead of displaying 100 it will display 1.00 and so on....


Thanks
Image
B
26
S
4
G
3
Posts: 147
Reputation: 4,875

Post » Tue Mar 10, 2015 4:34 am

In the following, assume the variable valueToAdd is a string value representation of the number you want to add and total is a float value. Using a string, you can check for an existing decimal with the find() method. Given this information, you would use the following to set the total amount after the addtion.

set total to total + (find(valueToAdd, ".") = -1 ? float(valueToAdd) / 100 : float(valueToAdd))

What this does is, first checks if a decimal is present in the valueToAdd (find(valueToAdd, ".") = -1). If the result of the find is -1, no decimal is found so we divide by 100 (float(valueToAdd) / 100). If the result of the find is > -1 the string already contains a decimal so, no need to divide.

To get more information about the statement, read the ?: section of the expressions tutorial here:
https://www.scirra.com/tutorials/77/nat ... uct-2#h2a0

I hope this gets you what you need and good luck with your project.
B
38
S
12
G
11
Posts: 331
Reputation: 7,712

Post » Tue Mar 10, 2015 12:43 pm

FragFather wrote:In the following, assume the variable valueToAdd is a string value representation of the number you want to add and total is a float value. Using a string, you can check for an existing decimal with the find() method. Given this information, you would use the following to set the total amount after the addtion.

set total to total + (find(valueToAdd, ".") = -1 ? float(valueToAdd) / 100 : float(valueToAdd))

What this does is, first checks if a decimal is present in the valueToAdd (find(valueToAdd, ".") = -1). If the result of the find is -1, no decimal is found so we divide by 100 (float(valueToAdd) / 100). If the result of the find is > -1 the string already contains a decimal so, no need to divide.

To get more information about the statement, read the ?: section of the expressions tutorial here:
https://www.scirra.com/tutorials/77/nat ... uct-2#h2a0

I hope this gets you what you need and good luck with your project.




Thanks! that is exactly what I was looking for, I know it can be done by capping the quarters,nickles,dimes, variables at 100, and then setting one for dollars every time they reach 100. but this is much more professional.

Thanks again.
Image
B
26
S
4
G
3
Posts: 147
Reputation: 4,875


Return to How do I....?

Who is online

Users browsing this forum: alajajm and 9 guests