0 votes
by (140 points)

Like the question says, I need help setting up a money conversion system. 100¢ to a dollar sorta thing


(set: $copper to 0)
(set: $silver to 0)
(set: $gold to 0)
(if: $copper >= 100

This is as far as I've gotten, but I'm not sure how to continue it or if I'm doing it right. one-hundred copper should equal one silver and one-hundred silver should equal one gold.

2 Answers

0 votes
by (153k points)

It is common in commercial financial systems to story currency using the smallest unit, and then to calculate the larger units only when necessary. (like when displaying the value in the different units)

eg, Banks may store a value like $1234.56 as 123456 cents.

I suggest you do the same, except in your case store the value as the total number of coppers and only calculate the individual number of gold, silver, and copper peices as needed.

(set: $money to 123456)\
You have a total of $money units of money.

(set: _gold to (floor: $money / 10000))\
(set: _silver to (floor: ($money % 10000) / 100))
(set: _copper to ($money % 100))\
It consists of: _gold`g`, _silver`s`, and _copper`c` peices.


0 votes
by (6.2k points)

Converting copper to silver:

(set: $silver to $copper / 100)

Converting silver to gold:

(set: $gold to $silver / 100)


by (153k points)

Your solution needs to be modified to handle the situation where the resulting $silver & $gold values contain a decimal point number.

eg. 1234 copper => 12.34 silver

You can't have 0.34 of a silver piece so you need remove everything after the decimal point so that 12.34 silver becomes 12 silver.

You have two main methods you can use to do this trunctation action:

1. The JavaScript Math.trunc() function.

(set: $copper to 123456)
(set: $silver to Math.trunc($copper / 100))
(set: $gold to Math.trunc($silver / 100))

$copper copper => $silver silver.
$silver silver => $gold gold.

2. The Harlowe (floor:) macro (if the result of the divide is a positive number)

(set: $copper to 123456)
(set: $silver to (floor: $copper / 100))
(set: $gold to (floor: $silver / 100))

$copper copper => $silver silver.
$silver silver => $gold gold.

note: Neither of the above examples handles the need to reduce:
a. the original $copper amount after some of it has been converted to silver pieces.
b. the calculated $silver amount after some of it has been converted to gold pieces.

eg. 123456 copper pieces after conversion should equal 12 gold 34 silver 56 copper.

by (6.2k points)
This is true. Thanks I didn't notice this at first.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.