Fix number formatting for 1000, 10000, ...
Some checks are pending
test-and-build / test-and-build (push) Waiting to run

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-07-23 13:47:03 +02:00
parent 2b23f1488c
commit 36359bdcb8
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E

View file

@ -12,15 +12,15 @@ export function formatNumber(number, thousandSep = ' ', decimalSep = '.', places
return result + number.toFixed(places) return result + number.toFixed(places)
} }
let place = Math.ceil(Math.log10(number)) const place = Math.floor(Math.log10(number))
if (place < 3) { if (place < 3) {
return result + number.toFixed(places).replace('.', decimalSep) return result + number.toFixed(places).replace('.', decimalSep)
} }
while (place--) { for (let i = place; i >= 0; i--) {
result += number / 10 ** place % 10 | 0 result += number / 10 ** i % 10 | 0
if (place > 0 && place % 3 === 0) { if (i > 0 && i % 3 === 0) {
result += thousandSep result += thousandSep
} }
} }