I've got a game with a wallet that keeps track of how much money the player has, and players can buy things in increments of 25 cents. However, I can't get the wallet to consistently display to two decimal places, so when the player has $1.50 it displays as $1.5.
I've tried using .toFixed(2):
(set: $money to (it-1.75).toFixed(2))
but then when the player tries to buy things they get an "undefined" error and the wall. It seems that these variables are being interpreted as strings instead of numbers, and that's why the math isn't working, so I tried using Number:
(set: $money to Number((it-1.75).toFixed(2)))
This does the math, but then it goes back to dropping the zeros on numbers like 1.50 (so I'm back to my original problem).
I'm working in Harlowe, and most of the discussion on decimal points seems to be in Sugarcube. Any folks working with decimal places in Harlowe have any advice?
Comments
I suggest tracking the lowest common denomination of currency in your world, which seems to be cents in this case, and only converting to dollars when printing for the player.
So, for example, to set $money to $2.50: And to subtract $1.70 from that:
To make printing cents out in dollars for the player easier, I'd suggest setting up a function to handle it. For example, add something like the following to your Story JavaScript: And using it:
The whole point of my previous original reply is that you cannot depend upon floating point numbers for exact accuracy, so in cases where you want/need it—e.g. with currency—don't use floating point numbers. In this case the suggestion was to simply track the player's money via the lowest common denomination, cents, so that whole/integer numbers could be used.
All the asDollars() function does is to take a given number of cents and return a string representation of the equivalent number of dollars and cents—format: $#+.##. It accomplishes this by shifting the decimal place using the Stupid Scientific Notation Trick™—it does not use division, so as to avoid floating point conversation.
If you've tried the function and are seeing results like $42.44, then you've passed it something which would generate that representation—e.g. 4244.
Could you clarify your question?
I was just wondering if it were possible to make it only output the dollar parts, plus .00 cents. (Since my character is wearing a wallet, without really any room for spare change).
That's why I set my <<set $cash to random(2000, 6000)>> in the first place, but I didn't actually realize that it'd pick random cents, as well
Alternatively, since you only wish to track whole dollars, just do that. If you want to print the player's money in dollars & cents format, even though you're only using dollars, just do something like the following: Turning that into a widget or function would be trivial, if desired.