Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix(formatting): round compact numbers with Intl; preserve legacy k casing (closes #59454) (#61645)

Lodewijk Wensveenb0687c26d2d4
diff --git a/frontend/src/metabase/lib/formatting/numbers.tsx b/frontend/src/metabase/lib/formatting/numbers.tsx
index 30e88a4229c..97716ff044d 100644
--- a/frontend/src/metabase/lib/formatting/numbers.tsx
+++ b/frontend/src/metabase/lib/formatting/numbers.tsx
@@ -1,4 +1,3 @@
-import Humanize from "humanize-plus";
import type { ReactNode } from "react";
import { COMPACT_CURRENCY_OPTIONS, getCurrencySymbol } from "./currency";
@@ -263,15 +262,17 @@ function _formatNumberCompact(
}
}
} else if (typeof value === "number") {
- // 1 => 1
- // 1000 => 1K
const isDefaultDecimalCount =
options.maximumFractionDigits ===
DEFAULT_NUMBER_OPTIONS.maximumFractionDigits;
- formatted = Humanize.compactInteger(
- Math.round(value),
- isDefaultDecimalCount ? 1 : options.maximumFractionDigits,
- );
+ const decimals =
+ options.maximumFractionDigits == null
+ ? 1
+ : isDefaultDecimalCount
+ ? 1
+ : options.maximumFractionDigits;
+
+ formatted = compactNumber(value, decimals);
} else {
formatted = PRECISION_NUMBER_FORMATTER.format(value);
}
@@ -347,3 +348,18 @@ function multiply(a: number | bigint, b: number) {
return Number(a) * b;
}
}
+
+function compactNumber(value: number, decimals: number): string {
+ const numberFormat = new Intl.NumberFormat("en", {
+ notation: "compact",
+ useGrouping: false,
+ maximumFractionDigits: decimals,
+ minimumFractionDigits: decimals,
+ });
+ let formatted = numberFormat.format(value);
+ // Match legacy casing for thousands
+ if (formatted.endsWith("K")) {
+ formatted = formatted.replace("K", "k");
+ }
+ return formatted;
+}
diff --git a/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts b/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts
index fce06316514..724cfdd6b18 100644
--- a/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts
+++ b/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts
@@ -61,6 +61,57 @@ describe("formatNumber", () => {
}),
).toEqual("1.000015e+0");
});
+
+ describe("formatNumber – compact rounding (metabase#59454)", () => {
+ it("rounds billions with 0 decimals", () => {
+ expect(
+ formatNumber(1_499_999_999, {
+ compact: true,
+ maximumFractionDigits: 0,
+ }),
+ ).toBe("1B");
+ expect(
+ formatNumber(1_500_000_000, {
+ compact: true,
+ maximumFractionDigits: 0,
+ }),
+ ).toBe("2B");
+ expect(
+ formatNumber(1_949_999_999, {
+ compact: true,
+ maximumFractionDigits: 0,
+ }),
+ ).toBe("2B");
+ });
+
+ it("carries to the next unit at 999.5M (0 decimals)", () => {
+ expect(
+ formatNumber(999_499_999, { compact: true, maximumFractionDigits: 0 }),
+ ).toBe("999M");
+ expect(
+ formatNumber(999_500_000, { compact: true, maximumFractionDigits: 0 }),
+ ).toBe("1B");
+ });
+
+ it("keeps sign with rounding", () => {
+ expect(
+ formatNumber(-1_950_000_000, {
+ compact: true,
+ maximumFractionDigits: 0,
+ }),
+ ).toBe("-2B");
+ });
+
+ it("respects explicit decimals when provided", () => {
+ expect(
+ formatNumber(1_950_000_000, {
+ compact: true,
+ minimumFractionDigits: 1,
+ maximumFractionDigits: 1,
+ }),
+ ).toBe("2.0B");
+ });
+ });
});
describe("formatNumber with scale (multiply function)", () => {