Scripting
Formatting Numbers

To make large numbers more readable, you can insert commas etc.., as thousands etc.., separators. JavaScript offers multiple ways to achieve this, from built-in locale-aware methods to custom regex solutions.

Dynamic, flexible

Generally the best way to format numbers is to be Locale aware. Canada and USA do it one way, Various other countries do it other ways.

The results vary from time to time as the browser engines get updated with more information. If you need it always done the same way, look at the section "Fixed format" below

If you want it done based on the browser's best understanding today of how to do it for a specific Locale, then specify the Locale:

const num = 1234567.89;
console.log(num.toLocaleString('en-US')); // "9,876,543,210.5"
console.log(num.toLocaleString('de-DE')); // "9.876.543.210,5"
console.log(num.toLocaleString("ar-EG")); // '٩٬٨٧٦٬٥٤٣٬٢١٠٫٥'

If you want it to be done based on the server or the user's locale (depending on where you are using it) then you can do:

console.long(numtoLocaleString()); // result depends on your Locale

The most common issue doing this though is that you were expecting it to be done as the user's locale and instead the Report or Even and Action or DataHub used the Server's locale. So usually you'll want to specify the locale.

You can also adjust the minimum number of decimals to show, so for example you might want 2 decimals when showing money data

console.log(num.toLocaleString('en-US', { minimumFractionDigits: 2 })); // "9,876,543,210.50"

You can also use the 'Intl function in most modern browser.

const formatter = new Intl.NumberFormat('en-US');
console.log(formatter.format(123456789)); // "123,456,789"
const currencyFormatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
console.log(currencyFormatter.format(1234567)); // "$1,234,567.00"

The toLocaleString() method of Number values returns a string with a language-sensitive representation of this number. In implementations with Intl.NumberFormat API support, this method delegates to Intl.NumberFormat.

Every time toLocaleString is called, it has to perform a search in a big database of localization strings, which is potentially inefficient. When the method is called many times with the same arguments, it is better to create an Intl.NumberFormat object and use its format() method, because a NumberFormat object remembers the arguments passed to it and may decide to cache a slice of the database, so future format calls can search for localization strings within a more constrained context.

Note: Most of the time, the formatting returned by toLocaleString() is consistent. However, the output may vary between implementations, even within the same locale — output variations are by design and allowed by the specification. It may also not be what you expect. For example, the string may use non-breaking spaces or be surrounded by bidirectional control characters. You should not compare the results of toLocaleString() to hardcoded constants.

Fixed Format:

There are times you don't care about being 'as up to date as possible' on your formatting, you want it to ALWAYS be 'this' format no matter who creates it, no matter what updates have happened.

For example, if you are adding ,'s into numbers in an Asset ID to make them easier to read and you want them to ALWAYS be ,'s (commas) - not ,'s (periods) when someone in one part of Canada does it and ' ' (space) or '.''s (periods) when someone in Quebec or BC (specific parts of Canada) does it.

function addCommas(value) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
console.log(addCommas(1234567890)); // "1,234,567,890"

If you number has been padded with zeros to a fixed length, 6 for the following example, you could also do the following extremely efficient:

console.log( num.str.slice(0, 3) + "," + str.slice(3);) // 004567 becomes 004,567

Undoubtedly there are more ways, and subtle differences in what you could use code wise. We aren't trying to teach you JavaScript, there are 10's of thousands of web sites that will do that. We are trying to show a very practical common use of JavaScript to get your appetite whet (or maybe to 100% solve your current need). Our professional services can provide you with JavaScript training, or you can use the vast resources of the web and AI tools to help you - often at no cost, and while it might take you longer, you may learn more through the experience. You can also use our professional services to 'just write it for you' if you don't think it is worth learning. We have some customers who write all their own scripts - often very fancy, and we have others that pay us to write all of them for them; also often fancy but also simple 'just do it'.