decimals & counting change

  • 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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)