diff options
| author | zousar <[email protected]> | 2025-04-11 00:25:20 -0600 |
|---|---|---|
| committer | zousar <[email protected]> | 2025-04-11 00:25:20 -0600 |
| commit | afda5c6345f4c0b274ae8084bfbd371169791c57 (patch) | |
| tree | ded3572c961377625cb516ed5b8374d2e2cbd99c /src/zenserver/frontend/html/util/friendly.js | |
| parent | 5.6.5 (diff) | |
| download | zen-afda5c6345f4c0b274ae8084bfbd371169791c57.tar.xz zen-afda5c6345f4c0b274ae8084bfbd371169791c57.zip | |
Avoid signed overflow using BigInt
Bias for use of BigInt when consuming integer fields in compact binary to avoid values showing up as negative due to overflow on the Number type.
Diffstat (limited to 'src/zenserver/frontend/html/util/friendly.js')
| -rw-r--r-- | src/zenserver/frontend/html/util/friendly.js | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/zenserver/frontend/html/util/friendly.js b/src/zenserver/frontend/html/util/friendly.js index b27721964..a15252faf 100644 --- a/src/zenserver/frontend/html/util/friendly.js +++ b/src/zenserver/frontend/html/util/friendly.js @@ -7,17 +7,17 @@ export class Friendly { static sep(value, prec=0) { - return (+value).toLocaleString("en", { + return (+Number(value)).toLocaleString("en", { style: "decimal", minimumFractionDigits : prec, maximumFractionDigits : prec, }); } - static k(x, p=0) { return Friendly.sep((x + 999) / Math.pow(10, 3)|0, p) + "K"; } - static m(x, p=1) { return Friendly.sep( x / Math.pow(10, 6), p) + "M"; } - static g(x, p=2) { return Friendly.sep( x / Math.pow(10, 9), p) + "G"; } - static kib(x, p=0) { return Friendly.sep((x + 1023) / (1 << 10)|0, p) + " KiB"; } - static mib(x, p=1) { return Friendly.sep( x / (1 << 20), p) + " MiB"; } - static gib(x, p=2) { return Friendly.sep( x / (1 << 30), p) + " GiB"; } + static k(x, p=0) { return Friendly.sep((BigInt(x) + 999n) / BigInt(Math.pow(10, 3))|0n, p) + "K"; } + static m(x, p=1) { return Friendly.sep( BigInt(x) / BigInt(Math.pow(10, 6)), p) + "M"; } + static g(x, p=2) { return Friendly.sep( BigInt(x) / BigInt(Math.pow(10, 9)), p) + "G"; } + static kib(x, p=0) { return Friendly.sep((BigInt(x) + 1023n) / (1n << 10n)|0n, p) + " KiB"; } + static mib(x, p=1) { return Friendly.sep( BigInt(x) / (1n << 20n), p) + " MiB"; } + static gib(x, p=2) { return Friendly.sep( BigInt(x) / (1n << 30n), p) + " GiB"; } } |