Skip to content

Commit f37941a

Browse files
fix: fix units calculations for number less than 1000
1 parent 7ff6454 commit f37941a

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/utils/dataFormatters/__test__/formatNumbers.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ describe('formatNumericValues', () => {
2121
const result = formatNumericValues(1024, 2048);
2222
expect(result).toEqual(['1', `2${UNBREAKABLE_GAP}k`]);
2323
});
24+
25+
it('should format values without units (less than 1000)', () => {
26+
const result1 = formatNumericValues(10, 20);
27+
expect(result1).toEqual(['10', `20`]);
28+
});
29+
2430
it('should format value with label if set', () => {
2531
const result = formatNumericValues(1024, 2048, undefined, undefined, true);
2632
expect(result).toEqual([`1${UNBREAKABLE_GAP}k`, `2${UNBREAKABLE_GAP}k`]);

src/utils/dataFormatters/formatNumber.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import type {FormatToSizeArgs, FormatValuesArgs} from './common';
55
import {formatNumber, roundToPrecision} from './dataFormatters';
66

77
const sizes = {
8+
noUnit: {
9+
value: 1,
10+
label: '',
11+
},
812
thousand: {
913
value: 1_000,
1014
label: i18n('label_thousand'),
@@ -26,9 +30,9 @@ const sizes = {
2630
export type Digits = keyof typeof sizes;
2731

2832
export const getNumberSizeUnit = (value: number) => {
29-
let size: Digits = 'thousand';
33+
let size: Digits = 'noUnit';
3034

31-
if (value > sizes.thousand.value) {
35+
if (value >= sizes.thousand.value) {
3236
size = 'thousand';
3337
}
3438
if (value >= sizes.million.value) {
@@ -51,7 +55,12 @@ const formatToSize = ({value, size = 'thousand', precision = 0}: FormatToSizeArg
5155
};
5256

5357
const addSizeLabel = (result: string, size: Digits, delimiter = UNBREAKABLE_GAP) => {
54-
return result + delimiter + sizes[size].label;
58+
const label = sizes[size].label;
59+
if (!label) {
60+
return result;
61+
}
62+
63+
return result + delimiter + label;
5564
};
5665

5766
export const formatNumberWithDigits = ({

0 commit comments

Comments
 (0)