diff --git a/frontend/src/metabase/lib/formatting.js b/frontend/src/metabase/lib/formatting.js index 8a4da5d4e8afa7836cdf5015d492753c26eac5d1..53b30d6a7cb932dc97b21e699234181c41186222 100644 --- a/frontend/src/metabase/lib/formatting.js +++ b/frontend/src/metabase/lib/formatting.js @@ -52,6 +52,7 @@ export type FormattingOptions = { const DEFAULT_NUMBER_OPTIONS: FormattingOptions = { compact: false, + maximumFractionDigits: 2, useGrouping: true, }; @@ -96,6 +97,7 @@ export function formatNumber(number: number, options: FormattingOptions = {}) { try { return number.toLocaleString(options.locale, options); } catch (e) { + console.warn("Error calling toLocaleString", e); return String(number); } } diff --git a/frontend/src/metabase/visualizations/visualizations/Scalar.jsx b/frontend/src/metabase/visualizations/visualizations/Scalar.jsx index 2895bbffe5486fb47365e09eb9f27de404f7bc22..fe6184e589f1b43dba6fa53187a6e1581d33e098 100644 --- a/frontend/src/metabase/visualizations/visualizations/Scalar.jsx +++ b/frontend/src/metabase/visualizations/visualizations/Scalar.jsx @@ -11,22 +11,22 @@ import { formatValue } from "metabase/lib/formatting"; import { TYPE } from "metabase/lib/types"; import cx from "classnames"; +import _ from "underscore"; import type { VisualizationProps } from "metabase/meta/types/Visualization"; function scalarSettingsToFormatOptions(settings) { - const decimals = parseFloat(settings["scalar.decimals"]); - const scale = parseFloat(settings["scalar.scale"]); - return { - column: column, + const formatOptions = { suffix: settings["scalar.suffix"], prefix: settings["scalar.prefix"], - scale: isNaN(scale) ? null : scale, + scale: parseFloat(settings["scalar.scale"]), locale: settings["scalar.locale"], useGrouping: !!settings["scalar.locale"], - minimumFractionDigits: isNaN(decimals) ? null : decimals, - maximumFractionDigits: isNaN(decimals) ? null : decimals, + minimumFractionDigits: parseFloat(settings["scalar.decimals"]), + maximumFractionDigits: parseFloat(settings["scalar.decimals"]), }; + // remove null options to allow for defaults + return _.pick(formatOptions, v => v != null && v !== "" && !isNaN(v)); } export default class Scalar extends Component { @@ -133,7 +133,10 @@ export default class Scalar extends Component { const value = rows[0] && rows[0][0]; const column = cols[0]; - const formatOptions = scalarSettingsToFormatOptions(settings); + const formatOptions = { + column, + ...scalarSettingsToFormatOptions(settings), + }; const fullScalarValue = formatValue(value, formatOptions); const compactScalarValue = isSmall