Displaying a comma in a number

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

Thanks, looks a lot more compact now.

Looks like 3.2.1 breaks this. I just get an error message now.

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))

Thanks, that worked great.