From 5a0076779a4bafbaf86ce3d0bbcfd72d1df809b3 Mon Sep 17 00:00:00 2001 From: thecashewtrader Date: Sat, 22 Oct 2022 21:24:44 +0530 Subject: [PATCH] Localize numbers using Internalisation API --- src/main.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/main.js b/src/main.js index a83a8d46..66bbb0eb 100644 --- a/src/main.js +++ b/src/main.js @@ -88,20 +88,8 @@ const mixin = { return str; }, numberFormat(num) { - const digits = 2; - const si = [ - { 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; + const formatter = Intl.NumberFormat(undefined, { notation: "compact" }); + return formatter.format(num); }, addCommas(num) { num = parseInt(num);