From bf96683075402a27ab30547d3efc744fbab3626a Mon Sep 17 00:00:00 2001 From: Phil DeOrsey Date: Thu, 26 Sep 2024 11:06:54 -0400 Subject: [PATCH 1/2] Add an option to force number format to display a fixed number of decimal places --- src/arithmetic.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/arithmetic.ts b/src/arithmetic.ts index a47df92..ddff65a 100644 --- a/src/arithmetic.ts +++ b/src/arithmetic.ts @@ -47,13 +47,13 @@ function addThousandSeparators(x: string) { return n + (dec ? `.${dec}` : ''); } -function addPowerSuffix(n: number, places = 6) { - if (!places) return `${n}`; +function addPowerSuffix(n: number, places = 6, fixed = 0) { + if (!places) return n.toFixed(fixed); // Trim short numbers to the appropriate number of decimal places. const digits = (`${Math.abs(Math.floor(n))}`).length; const chars = digits + (n < 0 ? 1 : 0); - if (chars <= places) return `${round(n, places - chars)}`; + if (chars <= places) return fixed ? n.toFixed(fixed) : `${round(n, places - chars)}`; // Append a power suffix to longer numbers. const x = Math.floor(Math.log10(Math.abs(n)) / 3); @@ -67,20 +67,20 @@ function addPowerSuffix(n: number, places = 6) { * adding thousands separators. `places` is the number of digits to show in the * result. */ -export function numberFormat(n: number, places = 0, separators = true) { - const str = addPowerSuffix(n, places).replace('-', '–'); +export function numberFormat(n: number, places = 0, separators = true, fixed = 0) { + const str = addPowerSuffix(n, places, fixed).replace('-', '–'); return separators ? addThousandSeparators(str) : str; } -export function scientificFormat(value: number, places = 6) { +export function scientificFormat(value: number, places = 6, fixed = 0) { const abs = Math.abs(value); if (isBetween(abs, Math.pow(10, -places), Math.pow(10, places))) { - return numberFormat(value, places); + return numberFormat(value, places, true, fixed); } // TODO Decide how we want to handle these special cases if (abs > Number.MAX_VALUE) return `${Math.sign(value) < 0 ? '–' : ''}∞`; - if (abs < Number.MIN_VALUE) return '0'; + if (abs < Number.MIN_VALUE) return Number(0).toFixed(fixed); const [str, exponent] = value.toExponential().split('e'); const top = exponent.replace('+', '').replace('-', '–'); From a5bf5d8d7157864f99e62be498b9cbf5acf1843e Mon Sep 17 00:00:00 2001 From: Phil DeOrsey Date: Tue, 8 Oct 2024 09:55:26 -0400 Subject: [PATCH 2/2] make fixed start at undefined instead of 0 --- src/arithmetic.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/arithmetic.ts b/src/arithmetic.ts index ddff65a..9f6f14d 100644 --- a/src/arithmetic.ts +++ b/src/arithmetic.ts @@ -47,13 +47,13 @@ function addThousandSeparators(x: string) { return n + (dec ? `.${dec}` : ''); } -function addPowerSuffix(n: number, places = 6, fixed = 0) { - if (!places) return n.toFixed(fixed); +function addPowerSuffix(n: number, places = 6, fixed?: number) { + if (!places) return fixed !== undefined ? n.toFixed(fixed) : `${n}`; // Trim short numbers to the appropriate number of decimal places. const digits = (`${Math.abs(Math.floor(n))}`).length; const chars = digits + (n < 0 ? 1 : 0); - if (chars <= places) return fixed ? n.toFixed(fixed) : `${round(n, places - chars)}`; + if (chars <= places) return fixed !== undefined ? n.toFixed(fixed) : `${round(n, places - chars)}`; // Append a power suffix to longer numbers. const x = Math.floor(Math.log10(Math.abs(n)) / 3); @@ -67,12 +67,12 @@ function addPowerSuffix(n: number, places = 6, fixed = 0) { * adding thousands separators. `places` is the number of digits to show in the * result. */ -export function numberFormat(n: number, places = 0, separators = true, fixed = 0) { +export function numberFormat(n: number, places = 0, separators = true, fixed?: number) { const str = addPowerSuffix(n, places, fixed).replace('-', '–'); return separators ? addThousandSeparators(str) : str; } -export function scientificFormat(value: number, places = 6, fixed = 0) { +export function scientificFormat(value: number, places = 6, fixed?: number) { const abs = Math.abs(value); if (isBetween(abs, Math.pow(10, -places), Math.pow(10, places))) { return numberFormat(value, places, true, fixed); @@ -80,7 +80,7 @@ export function scientificFormat(value: number, places = 6, fixed = 0) { // TODO Decide how we want to handle these special cases if (abs > Number.MAX_VALUE) return `${Math.sign(value) < 0 ? '–' : ''}∞`; - if (abs < Number.MIN_VALUE) return Number(0).toFixed(fixed); + if (abs < Number.MIN_VALUE) return fixed !== undefined ? Number(0).toFixed(fixed) : '0'; const [str, exponent] = value.toExponential().split('e'); const top = exponent.replace('+', '').replace('-', '–');