Pretty simple. I'm trying to figure out how to print a variable in such a way that it has commas inserted for readability if the number is greater than 3 digits. I understand that the simplest way to do this probably involves converting to string and doing some wizardry from there, but I'm too much of an idiot to figure it out in a vacuum. Anyone have an example of this, maybe a function or script that's already been made?
I tried searching for it, since I'm sure this has been addressed before, but got way too many unrelated threads.
Comments
A. Those that take into consideration that some nationalities (locales) format large & decimal numbers differently.
eg. Germans uses comma as decimal separator and period for thousands.
B. Those that enforce the UK/US standard formatting.
eg. period as decimal separator and comma for thousands.
The second choice that needs to be made is how to implement the custom function within SugarCube, two options are.
A. Add the custom function to SugarCube's setup object.
B. Extend the existing Javascript Number class prototype.
The following example demonstrates one way to implement a custom function on the setup object that takes into consideration the locale of the Reader by using the Javascript Intl.NumberFormat function. This code needs to be places within a script tagged passage. You can use the following code with a standard Passage to test the new setup.formatNumber(number) function.
Here's a version which will return the original value if either the Intl object isn't supported or a non-number is given:
Many of the community members that supply answers (to peoples questions) can also be found on the new Q/A site, so I would not worry about the Q/A site being "less helpful'.