The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(front): move abbreviated numbers to the next suffix when rounding reaches 1000 (#26880)
Rup Sarmahc3d180f5cc5f
diff --git a/packages/twenty-front/src/modules/localization/utils/__tests__/formatNumber.test.ts b/packages/twenty-front/src/modules/localization/utils/__tests__/formatNumber.test.tsindex 4980536a0bf..59345fb7ad8 100644--- a/packages/twenty-front/src/modules/localization/utils/__tests__/formatNumber.test.ts+++ b/packages/twenty-front/src/modules/localization/utils/__tests__/formatNumber.test.ts@@ -121,5 +121,32 @@ describe('formatNumber', () => {'-1.5k',);});++ it('should move to the next suffix when rounding reaches 1000', () => {+ expect(formatNumber(999999, { abbreviate: true, decimals: 1 })).toEqual(+ '1M',+ );+ expect(formatNumber(999500, { abbreviate: true })).toEqual('1M');+ expect(+ formatNumber(999999999, { abbreviate: true, decimals: 2 }),+ ).toEqual('1B');+ expect(formatNumber(999.6, { abbreviate: true })).toEqual('1k');+ expect(formatNumber(-999960, { abbreviate: true, decimals: 1 })).toEqual(+ '-1M',+ );+ expect(+ formatNumber(999.9999995, { abbreviate: true, decimals: 6 }),+ ).toEqual('1k');+ expect(+ formatNumber(999999999.5, { abbreviate: true, decimals: 6 }),+ ).toEqual('1B');+ });++ it('should keep the current suffix just below the rounding boundary', () => {+ expect(formatNumber(999940, { abbreviate: true, decimals: 1 })).toEqual(+ '999.9k',+ );+ expect(formatNumber(999.4, { abbreviate: true })).toEqual('999');+ });});});diff --git a/packages/twenty-front/src/modules/localization/utils/formatNumber.ts b/packages/twenty-front/src/modules/localization/utils/formatNumber.tsindex ef96b0bced8..617152f210f 100644--- a/packages/twenty-front/src/modules/localization/utils/formatNumber.ts+++ b/packages/twenty-front/src/modules/localization/utils/formatNumber.ts@@ -25,6 +25,30 @@ const defaultOptions: Required<FormatNumberOptions> = {format: NumberFormat.COMMAS_AND_DOT,};+const ABBREVIATION_SUFFIX_BY_THRESHOLD: Record<number, string> = {+ 1e9: 'B',+ 1e6: 'M',+ 1e3: 'k',+ 1: '',+};++const getAbbreviationDivisor = (absoluteValue: number) =>+ [1e9, 1e6, 1e3].find((divisor) => absoluteValue >= divisor) ?? 1;++const roundToDecimals = (value: number, decimals: number) =>+ Number(+ value.toLocaleString('en-US', {+ maximumFractionDigits: decimals,+ useGrouping: false,+ }),+ );++const roundToDisplayedPrecision = (absoluteValue: number, decimals: number) => {+ const divisor = getAbbreviationDivisor(absoluteValue);++ return roundToDecimals(absoluteValue / divisor, decimals) * divisor;+};+const getLocaleForFormat = (format?: NumberFormat): string => {if (!format) {return defaultOptions.locale;@@ -58,29 +82,18 @@ export const formatNumber = (const locale = getLocaleForFormat(options.format);if (options.abbreviate) {- const abs = Math.abs(value);- let suffix = '';- let divisor = 1;-- if (abs >= 1e9) {- suffix = 'B';- divisor = 1e9;- } else if (abs >= 1e6) {- suffix = 'M';- divisor = 1e6;- } else if (abs >= 1e3) {- suffix = 'k';- divisor = 1e3;- }-- if (divisor !== 1) {- const base = value / divisor;- const formatted = base.toLocaleString(locale, {+ const roundedAbsoluteValue = roundToDisplayedPrecision(+ Math.abs(value),+ options.decimals,+ );+ const divisor = getAbbreviationDivisor(roundedAbsoluteValue);++ return (+ (value / divisor).toLocaleString(locale, {minimumFractionDigits: 0,maximumFractionDigits: options.decimals,- });- return formatted + suffix;- }+ }) + ABBREVIATION_SUFFIX_BY_THRESHOLD[divisor]+ );}return value.toLocaleString(locale, {