jamesarc
(James)
December 28, 2020, 7:31am
1
Twine Version: 2.3.9
Story Format: Harlowe 3.1.0
Is there a command to show a variables value with a comma? i.e. 20000 as 20,000
I did the following to get around this for now:
(set: $treasury to 20000)
(if: $treasury > 9999)[
(print: (str: $treasury)'s 1stto4thlast),(print: (str: $treasury)'s 3rdlasttolast)
]
(else:)[
$treasury
]
You can use the Javascript toLocaleString() function:
(set: $treasury to 20000)
(print: '$' + $treasury.toLocaleString())
That’ll format the number into whatever the local numerical format is. In the U.S., the result would be $20,000. (If you don’t want the dollar sign, get rid of the '$' +
in the second line.)
jamesarc
(James)
December 29, 2020, 11:53am
3
Thanks, looks a lot more compact now.
jamesarc
(James)
January 22, 2021, 5:53am
4
Looks like 3.2.1 breaks this. I just get an error message now.
JoshGrams
(Josh Grams)
January 22, 2021, 1:36pm
5
Ugh, so it does. That’s annoying. Give this a try:
(set: $thousands to (macro: num-type _n, [
(set: _d to (split: '', (str: _n)))
(set: _e to (altered: via it + (cond: (_d's length - pos) % 3 is 0 and pos < _d's length, ',', ''), ..._d))
(output-data: (joined: '', ..._e))
]))
Use it with something like:
(set: $treasury to 20000)
(print: ($thousands: $treasury))
jamesarc
(James)
January 31, 2021, 10:04am
6
Thanks, that worked great.