Skip to content

The open alternative to Salesforce, designed for AI.

fix(twenty-shared): derive short-number suffix from the rounded value (#21591)

Yash Singh505094650f42
diff --git a/packages/twenty-shared/src/utils/__tests__/formatToShortNumber.test.ts b/packages/twenty-shared/src/utils/__tests__/formatToShortNumber.test.ts
index e2504cea10b..05fe0e29cd9 100644
--- a/packages/twenty-shared/src/utils/__tests__/formatToShortNumber.test.ts
+++ b/packages/twenty-shared/src/utils/__tests__/formatToShortNumber.test.ts
@@ -30,6 +30,17 @@ describe('formatToShortNumber', () => {
expect(formatToShortNumber(0)).toBe('0');
});
+ it('promotes to the next unit when rounding reaches the boundary', () => {
+ expect(formatToShortNumber(999999)).toBe('1m');
+ expect(formatToShortNumber(999950)).toBe('1m');
+ expect(formatToShortNumber(999999999)).toBe('1b');
+ });
+
+ it('does not promote just below a rounding boundary', () => {
+ expect(formatToShortNumber(999949)).toBe('999.9k');
+ expect(formatToShortNumber(999)).toBe('999');
+ });
+
describe('negative numbers', () => {
it('formats negative numbers less than 1000 correctly', () => {
expect(formatToShortNumber(-500)).toBe('-500');
@@ -50,5 +61,9 @@ describe('formatToShortNumber', () => {
expect(formatToShortNumber(-1200000000)).toBe('-1.2b');
expect(formatToShortNumber(-987654321987)).toBe('-987.7b');
});
+
+ it('promotes negative numbers when rounding reaches the boundary', () => {
+ expect(formatToShortNumber(-999999)).toBe('-1m');
+ });
});
});
diff --git a/packages/twenty-shared/src/utils/format/formatToShortNumber.ts b/packages/twenty-shared/src/utils/format/formatToShortNumber.ts
index 43359643f42..fb83792ab58 100644
--- a/packages/twenty-shared/src/utils/format/formatToShortNumber.ts
+++ b/packages/twenty-shared/src/utils/format/formatToShortNumber.ts
@@ -2,25 +2,17 @@ export const formatToShortNumber = (amount: number) => {
const sign = amount < 0 ? '-' : '';
const absoluteAmount = Math.abs(amount);
- if (absoluteAmount < 1000) {
- return sign + absoluteAmount.toFixed(1).replace(/\.?0+$/, '');
- }
+ const format = (scaled: number, suffix: string) =>
+ sign + scaled.toFixed(1).replace(/\.?0+$/, '') + suffix;
- if (absoluteAmount < 1_000_000) {
- return (
- sign + (absoluteAmount / 1000).toFixed(1).replace(/\.?0+$/, '') + 'k'
- );
+ if (Number(absoluteAmount.toFixed(1)) < 1000) {
+ return format(absoluteAmount, '');
}
-
- if (absoluteAmount < 1_000_000_000) {
- return (
- sign + (absoluteAmount / 1_000_000).toFixed(1).replace(/\.?0+$/, '') + 'm'
- );
+ if (Number((absoluteAmount / 1_000).toFixed(1)) < 1000) {
+ return format(absoluteAmount / 1_000, 'k');
}
-
- return (
- sign +
- (absoluteAmount / 1_000_000_000).toFixed(1).replace(/\.?0+$/, '') +
- 'b'
- );
+ if (Number((absoluteAmount / 1_000_000).toFixed(1)) < 1000) {
+ return format(absoluteAmount / 1_000_000, 'm');
+ }
+ return format(absoluteAmount / 1_000_000_000, 'b');
};