# Decimal points for currency

In my game I’ve been trying to usea currency variable with a decimal point (to give a more realistic feel i.e. pounds and pence/dollars and cents) but whenever something is bought (so set currency -xx.xx) it ends up with a lot of extra spaces after the decimal place in the stats screen, generally not rounding up the final penny i.e. currency becomes xx.xx999999999999.

I’ve had a look round the forum, but couldn’t really spot anything similar, so was wondering if anyone else has got a work around. I’ve got a few ideas but not entirely sure how to impliment them.

Is there some way to set it so that it only displays two decimal places (and rounds up) or will it be better to set a pounds variable and a pence variable (with some form of clause to deal with pence going below 0 to make the maths work - which feels it could end up with some clunky code).

I also thought that there may be a way to display a decimal point in the stats screen, but be working with full numbers in the code (so the player sees xx.xx but the coding reads *create currency xxxx and when you get to buying something *set currency -xxxx).

If anyone has any ideas, it would be great to hear your thoughts! Thanks

1 Like

I haven’t experimented much with decimals in CS, but if you’re happy to convert between types of currency instead, this may be of help:

1 Like

Ah great, yes that was sort of what I was thinking, but hadn’t quite worked out the maths yet as was trying to work out if I could tweak the decimals. Thanks CJW!

Weirdly I noticed that the rounding up issue seems to resolve itself later on, and is only an issue if you check the stats directly after something has been bought, but fine a bit later on, so may be a glitch with testing. I’ll have an experiment to see if this replicates though…

Thanks again!

You could try this:

``````\*set money 2.75

\*temp dollars
\*set dollars round(money - 0.5)
\${dollars}
``````

*temp cents
*set cents (money - dollars)
\${cents}

So you can write “2 dollars and 75 cents” in your stats page.
Money: {dollars}.{cents}
Money: 2.75

1 Like

I think I’ve worked this out well enough, and if there’s a flaw somewhere let me know. This method requires you to use the lowest form of currency to do all of your calculations and then separates it out to be aesthetically correct.

Currency Example

Code:

``````*title Currency Management Example

*create cash 0
*create dollars 0
*comment "cash" is the stat you'll be doing all of your changes to. "dollars" is just for display purposes.

*temp change 0
*label start
*set dollars (cash/100)
*comment [[The last line puts your cash into a dollar amount, hopefully in the xx.xx format. However depending on the amount, it could wind up being x.x or even just x. The following *if statements should fix that problem.]]
You have
*if cash = 0
\$0.00
*goto startchoice
*elsif ((cash % 100) = 0)
\$\${dollars}.00
*goto startchoice
*elsif ((cash % 10) = 0)
\$\${dollars}0
*goto startchoice
*else
\$\${dollars}
*goto startchoice
*label startchoice
*choice
Enter value (in cents) to add:
*input_number change 1 999999999
*set cash + change
*goto start
#Subract money
Enter value (in cents) to subtract:
*input_number change 1 999999999
*set cash - change
*goto start
#Lose all money
*set cash 0
*goto start
``````

The stats page uses a slightly different setup with the negative before the dollar sign instead of after it:

``````*if dollars < 0
*set dollars (0 - dollars)
Debt:
*if ((cash % 100) = 0)
-\$\${dollars}.00
*goto statend
*elsif ((cash % 10) = 0)
-\$\${dollars}0
*goto statend
*else
-\$\${dollars}
*goto statend
*if dollars >= 0
Money:
*if cash = 0
\$0.00
*goto statend
*elsif ((cash % 100) = 0)
\$\${dollars}.00
*goto statend
*elsif ((cash % 10) = 0)
\$\${dollars}0
*goto statend
*else
\$\${dollars}
*goto statend
*label statend
``````

Didn’t think I’d ever use the modulo operator (the lone “%” in those *if statements) for anything in ChoiceScript, but there you go.

1 Like