Formatting Numbers

Changing how a number is displayed without changing the number.

Number formatting controls how a number is displayed, such as thousands separators, decimal places, currency symbols, and percentages, without changing the underlying value. 1234567.891 is stored once and can be shown as 1,234,567.891, 1.234.567,891, or $1,234,567.89 depending on who is reading it.

Why it matters when importing data

Imports go wrong in both directions. Files arrive with numbers already formatted, such as $1,234.50 or 1.234,50, and have to be parsed back into a number before they are stored. And numbers shown in your product need formatting for the user's locale. Mixing the two, storing formatted text instead of numbers, breaks every sum and sort.

Formatting Numbers in JavaScript

In JavaScript, Intl.NumberFormat handles separators, currencies, and percentages for any locale. The value stays a number; only the displayed text changes:

// Formatting changes how a number is displayed. The stored value stays the same.
const examples = [
  { value: 1234567.891, locale: "en-US", options: {} },
  { value: 1234567.891, locale: "de-DE", options: {} },
  {
    value: 1234567.891,
    locale: "en-US",
    options: { style: "currency", currency: "USD" },
  },
  {
    value: 1234567.891,
    locale: "en-GB",
    options: { style: "currency", currency: "GBP" },
  },
  {
    value: -42.5,
    locale: "en-US",
    options: { style: "currency", currency: "USD", currencySign: "accounting" },
  },
  {
    value: 0.0825,
    locale: "en-US",
    options: { style: "percent", maximumFractionDigits: 1 },
  },
  { value: 1234567.891, locale: "en-US", options: { notation: "compact" } },
];

const results = examples.map(({ value, locale, options }) => ({
  value,
  locale,
  style: options.style ?? options.notation ?? "decimal",
  formatted: new Intl.NumberFormat(locale, options).format(value),
}));
console.table(results);

Output

Stored valueLocaleStyleDisplayed as
1234567.891en-USdecimal1,234,567.891
1234567.891de-DEdecimal1.234.567,891
1234567.891en-UScurrency$1,234,567.89
1234567.891en-GBcurrency£1,234,567.89
-42.5en-UScurrency($42.50)
0.0825en-USpercent8.3%
1234567.891en-UScompact1.2M

Edge cases to handle

  • Comma or dot. 1.234 is one thousand two hundred thirty-four in Germany and just over one in the US. Know the source locale before parsing.
  • Percent inputs. A file might contain 8.25, 8.25%, or 0.0825 for the same rate. Decide which one the field expects.
  • Negative numbers. Accounting formats show negatives in parentheses, such as ($42.50). Convert them to a minus sign when parsing.
  • Rounding. Format for display, but store full precision. Rounding before storing loses data you cannot get back.
  • Comparing formatted text. fr-FR output uses narrow no-break spaces, so 1 234 567,89 € typed by hand will not equal it. Never compare formatted strings.
  • Floating point. 0.1 + 0.2 is not exactly 0.3. Store money as integer cents or a decimal type when exact totals matter.

How Dromo handles formatting numbers

Dromo's number field type parses common number formats in the file, such as values with thousands separators, adds a validation error to any value it cannot parse, and outputs a real number. Presets such as usd, usd_accounting, eur, gbp, percent, and decimal_2 control display, displayFormat and outputFormat customize it further, and min and max validate the range.

Frequently asked questions

What is number formatting?

Number formatting changes how a number is displayed, such as adding thousands separators, a currency symbol, or a percent sign, without changing the value that is stored or calculated with.

How do I format a number as currency in JavaScript?

Use new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(value). Change the locale and currency code for other markets, for example "de-DE" and "EUR".