Number display formatting (#1556)

* number display formatting

* - add tests
This commit is contained in:
brendanlaschke
2023-09-13 11:12:25 +03:00
committed by GitHub
parent 7e6c9141f4
commit 84b474c3cc
7 changed files with 47 additions and 9 deletions

View File

@ -0,0 +1,17 @@
import { formatNumber } from '../number';
// This tests the en-US locale by default
describe('formatNumber', () => {
it(`Should format 123 correctly`, () => {
expect(formatNumber(123)).toEqual('123');
});
it(`Should format decimal numbers correctly`, () => {
expect(formatNumber(123.92)).toEqual('123.92');
});
it(`Should format large numbers correctly`, () => {
expect(formatNumber(1234567)).toEqual('1,234,567');
});
it(`Should format large numbers with a decimal point correctly`, () => {
expect(formatNumber(7654321.89)).toEqual('7,654,321.89');
});
});

View File

@ -0,0 +1,3 @@
export function formatNumber(value: number): string {
return value.toLocaleString();
}

View File

@ -1,4 +0,0 @@
export function formatNumber(value: number) {
// Formats the value to a string and add commas to it ex: 50,000 | 500,000
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}