Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Sugarcube 2] Printing large numbers with commas

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

  • edited July 2017
    There are a number of different methods you can use to achieve the effect you want but they basically break down into two categories, but no mater whichever method you choose you will need to Javascript to implement it.

    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.
    /* Format a number for display purposes using the Reader's default locale.
    *
    *	number:	the number to format.
    */
    if (!setup.formatNumber) {
    	setup.formatNumber = function (number) {
    		return new Intl.NumberFormat().format(number);
    	};
    }
    
    You can use the following code with a standard Passage to test the new setup.formatNumber(number) function.
    <<set $var to 12345.67>>
    
    var: <<print setup.formatNumber($var)>>
    
  • greyelf wrote: »
    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.
    /* Format a number for display purposes using the Reader's default locale.
    *
    *	number:	the number to format.
    */
    if (!setup.formatNumber) {
    	setup.formatNumber = function (number) {
    		return new Intl.NumberFormat().format(number);
    	};
    }
    
    Unfortunately, that code will fail in browsers which do not support the Intl object—mostly somewhat older browsers at this point, though there are a few current hold-outs, e.g. FF for Android.

    Here's a version which will return the original value if either the Intl object isn't supported or a non-number is given:
    (function () {
    	function hasIntlNumberFormat() {
    		try {
    			return typeof window.Intl.NumberFormat === 'function';
    		}
    		catch (ex) { /* no-op */ }
    
    		return false;
    	}
    
    	if (hasIntlNumberFormat()) {
    		setup.formatNumber = function (number) {
    			if (typeof number !== 'number') {
    				return number;
    			}
    
    			return new Intl.NumberFormat().format(number);
    		};
    	}
    	else {
    		setup.formatNumber = function (number) {
    			return number;
    		};
    	}
    })();
    
  • Unfortunately, that code will fail in browsers which do not support the Intl object—mostly somewhat older browsers at this point, though there are a few current hold-outs, e.g. FF for Android.
    Thank you for that explanation and the better implementation of the solution.
  • Thanks guys, awesome as always. Offtopic, but are there any plans to make a new forum to replace this one when it goes archive only soon? The Q&A system Klembot wants to push seems like a really sad replacement for the forums, and any place without you two would be a lot less helpful.
  • selenti wrote: »
    ...but are there any plans to make a new forum...
    Not that I'm aware of, although there is nothing stopping any pro-active individual (of group of such) from creating one of their own.
    selenti wrote: »
    ...and any place without you two would be a lot less helpful.
    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'.
Sign In or Register to comment.