Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number format can display fixed decimal places #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/arithmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@

function addThousandSeparators(x: string) {
let [n, dec] = x.split('.');
while (NUM_REGEX.test(n)) {

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
n = n.replace(NUM_REGEX, '$1,$2');

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '0'.
}
return n + (dec ? `.${dec}` : '');
}

function addPowerSuffix(n: number, places = 6) {
if (!places) return `${n}`;
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 `${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);
Expand All @@ -67,20 +67,20 @@
* 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?: number) {
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?: number) {
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 fixed !== undefined ? Number(0).toFixed(fixed) : '0';

const [str, exponent] = value.toExponential().split('e');
const top = exponent.replace('+', '').replace('-', '–');
Expand Down