Skip to content

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

respect decimals setting when compact formatting is enabled (#63457)

Aleksandr Lesnenkob5942d4f3661
diff --git a/frontend/src/metabase/lib/formatting/numbers.tsx b/frontend/src/metabase/lib/formatting/numbers.tsx
index 97716ff044d..c96e7413712 100644
--- a/frontend/src/metabase/lib/formatting/numbers.tsx
+++ b/frontend/src/metabase/lib/formatting/numbers.tsx
@@ -265,12 +265,18 @@ function _formatNumberCompact(
const isDefaultDecimalCount =
options.maximumFractionDigits ===
DEFAULT_NUMBER_OPTIONS.maximumFractionDigits;
- const decimals =
- options.maximumFractionDigits == null
- ? 1
- : isDefaultDecimalCount
+
+ let decimals: number | null =
+ typeof options.decimals === "number" && isFinite(options.decimals)
+ ? options.decimals
+ : null;
+
+ if (decimals == null) {
+ decimals =
+ options.maximumFractionDigits == null || isDefaultDecimalCount
? 1
: options.maximumFractionDigits;
+ }
formatted = compactNumber(value, decimals);
} else {
diff --git a/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts b/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts
index 724cfdd6b18..2492f8e0ccd 100644
--- a/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts
+++ b/frontend/src/metabase/lib/formatting/numbers.unit.spec.ts
@@ -62,6 +62,47 @@ describe("formatNumber", () => {
).toEqual("1.000015e+0");
});
+ describe("compact mode with decimals setting", () => {
+ it("should respect various decimal settings in compact mode (metabase#63145)", () => {
+ const value = 1234567;
+
+ expect(
+ formatNumber(value, {
+ compact: true,
+ decimals: 0,
+ }),
+ ).toBe("1M");
+
+ expect(
+ formatNumber(value, {
+ compact: true,
+ decimals: 1,
+ }),
+ ).toBe("1.2M");
+
+ expect(
+ formatNumber(value, {
+ compact: true,
+ decimals: 2,
+ }),
+ ).toBe("1.23M");
+
+ expect(
+ formatNumber(value, {
+ compact: true,
+ decimals: 3,
+ }),
+ ).toBe("1.235M");
+
+ expect(
+ formatNumber(value, {
+ compact: true,
+ decimals: 4,
+ }),
+ ).toBe("1.2346M");
+ });
+ });
+
describe("formatNumber – compact rounding (metabase#59454)", () => {
it("rounds billions with 0 decimals", () => {
expect(