'Localisation of large number symbols (K, M, B, T, etc.)

Hi there I am busy working on Shortening long numbers in javascript for graphing display purposes, such that a long number like: 12,345,678 becomes: 12.3M

My questions are as follows:

How do I localise this?

Can this be localised?

Does the INTL api have something for dealing with this?

Just to clarify, I have already written JS code to handle all of the BMK stuff and that works fine. My questions pertain only to the correct way (if any) to localise this.

Also to clarify, I am using the 'modern english' system that does not use things like milliard or billiard, nor do I care for adoption of such things due to their minority.

That being said if someone points me to a localisation solution that already takes care of things like milliard and billiard, etc. then I certainly won't say no.



Solution 1:[1]

By using the Intl.NumberFormat() constructor you can format your numbers with localisation as requested.

Here is an example in locales en ar fr es hi.

let op = {notation:"compact",
          compactDisplay:"short",  // use compact mode
          roundingMode:"ceil",     // rounding mode
          maximumFractionDigits:1  // use 1 decimal place
         };

console.log(new Intl.NumberFormat('en-GB',op).format(12345678));  // ? "12.3M"
console.log(new Intl.NumberFormat('es',op).format(12345678));      // ? "12,3 M"
console.log(new Intl.NumberFormat('ar',op).format(12345678));     // ? "12.3 ?????"
console.log(new Intl.NumberFormat('fr',op).format(12345678));     // ? "12,3 M"
console.log(new Intl.NumberFormat('hi',op).format(12345678));     // ? "1.2 ??"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohsen Alyafei