Merge pull request #1608 from thecashewtrader/number-localisation

Localize numbers using Internalisation API
This commit is contained in:
Kavin 2022-10-24 10:45:51 +01:00 committed by GitHub
commit 5f9ac9ed3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -88,20 +88,8 @@ const mixin = {
return str; return str;
}, },
numberFormat(num) { numberFormat(num) {
const digits = 2; const formatter = Intl.NumberFormat(undefined, { notation: "compact" });
const si = [ return formatter.format(num);
{ value: 1, symbol: "" },
{ value: 1e3, symbol: "k" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "B" },
];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
for (var i = si.length - 1; i > 0; i--) {
if (num >= si[i].value) {
break;
}
}
return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
}, },
addCommas(num) { addCommas(num) {
num = parseInt(num); num = parseInt(num);