Formatting Scientific Values in JavaScript

Scientific notation, significant figures, and SI prefixes with Intl.NumberFormat, for dashboards that show measurements

Measurement data breaks the usual number formatting. A sensor reports 0.0000123 and the UI shows 0.0000123, or worse, 1.23e-5 straight from String(). A large value shows up as 1234567.891 with more digits than the instrument can actually measure. Engineers and scientists expect values like these instead:

  • 4.20 × 10⁻⁴ for very small or very large values
  • 1.20, not 1.2, when the measurement is precise to three significant figures
  • 12.3 µA, not 0.0000123 A

All three are possible with the built-in Intl.NumberFormat, plus a few small helpers. No library needed.

Scientific notation with Intl

      
const sci = new Intl.NumberFormat('en-US', {
  notation: 'scientific',
  maximumSignificantDigits: 3
});

sci.format(0.000012345);  // "1.23E-5"

notation: 'scientific' does the hard part: rounding and choosing the exponent. But 1.23E-5 is programmer notation. On screen, people expect 1.23 × 10⁻⁵.

formatToParts returns the same output split into labeled pieces, so the exponent can be pulled out and rewritten:

      
sci.formatToParts(-0.000012345);
// [
//   { type: 'minusSign',         value: '-' },
//   { type: 'integer',           value: '1' },
//   { type: 'decimal',           value: '.' },
//   { type: 'fraction',          value: '23' },
//   { type: 'exponentSeparator', value: 'E' },
//   { type: 'exponentMinusSign', value: '-' },
//   { type: 'exponentInteger',   value: '5' }
// ]

A helper for × 10ⁿ

      
const SUPERSCRIPT = {
  '0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴',
  '5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹', '-': '⁻'
};

// Split Intl's output into the number part and the exponent
function split(value, notation, digits) {
  const parts = new Intl.NumberFormat('en-US', {
    notation,
    minimumSignificantDigits: digits,
    maximumSignificantDigits: digits
  }).formatToParts(value);

  let mantissa = '';
  let exponent = '';
  for (const part of parts) {
    if (part.type === 'exponentSeparator') continue;
    if (part.type === 'exponentMinusSign' || part.type === 'exponentInteger') exponent += part.value;
    else mantissa += part.value;
  }
  return { mantissa, exponent: Number(exponent) };
}

function toScientific(value, digits = 3) {
  const { mantissa, exponent } = split(value, 'scientific', digits);
  const superscript = [...String(exponent)].map(c => SUPERSCRIPT[c]).join('');
  return `${mantissa} × 10${superscript}`;
}

toScientific(0.000012345);    // "1.23 × 10⁻⁵"
toScientific(6.02214076e23);  // "6.02 × 10²³"

split is reused in the next two sections. Superscript characters keep the result plain text, so it works in tables, tooltips, chart labels, and CSV exports, with no HTML needed.

Significant figures: keep the trailing zeros

Notice that split sets both minimumSignificantDigits and maximumSignificantDigits. In measurement data, trailing zeros carry meaning:

      
const loose = new Intl.NumberFormat('en-US', { maximumSignificantDigits: 3 });
const fixed = new Intl.NumberFormat('en-US', {
  minimumSignificantDigits: 3,
  maximumSignificantDigits: 3
});

loose.format(1.2);  // "1.2"
fixed.format(1.2);  // "1.20"   precise to three figures
fixed.format(20);   // "20.0"

1.2 and 1.20 are different claims: the second says the instrument measured the hundredths place. Setting only the maximum silently drops that information.

Switching to scientific notation automatically

Most values read best as plain numbers. Only very small or very large ones need scientific notation:

      
function formatValue(value, { digits = 3, min = 0.001, max = 1e6 } = {}) {
  if (!Number.isFinite(value)) return '—';
  if (value === 0) return '0';

  // Decide on the rounded value, so 999,999 at 3 digits becomes 1.00 × 10⁶, not "1,000,000"
  const rounded = Math.abs(Number(value.toPrecision(digits)));
  if (rounded < min || rounded >= max) return toScientific(value, digits);

  return new Intl.NumberFormat('en-US', {
    minimumSignificantDigits: digits,
    maximumSignificantDigits: digits
  }).format(value);
}

formatValue(0.0042);     // "0.00420"
formatValue(0.00042);    // "4.20 × 10⁻⁴"
formatValue(123.456);    // "123"
formatValue(999999);     // "1.00 × 10⁶"
formatValue(NaN);        // "—"

The rounding check is the easy part to get wrong. 999999 is below one million, but rounded to three significant figures it displays as 1,000,000, exactly the kind of long number the threshold exists to avoid. Checking the rounded value fixes it. NaN and Infinity show a dash instead of leaking into the UI.

SI prefixes: 12.3 µA instead of 0.0000123 A

For values with units, prefixes like milli, micro, and kilo are often clearer than any exponent. notation: 'engineering' is the key: it's scientific notation with the exponent always a multiple of 3, which is exactly what SI prefixes are.

      
const PREFIXES = { '-12': 'p', '-9': 'n', '-6': 'µ', '-3': 'm', '0': '', '3': 'k', '6': 'M', '9': 'G' };

function formatWithUnit(value, unit, digits = 3) {
  const { mantissa, exponent } = split(value, 'engineering', digits);
  const prefix = PREFIXES[exponent];
  // Outside p…G there's no prefix, so fall back to scientific notation
  if (prefix === undefined) return `${toScientific(value, digits)} ${unit}`;
  return `${mantissa} ${prefix}${unit}`;
}

formatWithUnit(0.0000123, 'A');  // "12.3 µA"
formatWithUnit(0.042, 'A');      // "42.0 mA"
formatWithUnit(101325, 'Pa');    // "101 kPa"
formatWithUnit(999.96, 'Pa');    // "1.00 kPa"
formatWithUnit(2.5e-15, 'mol');  // "2.50 × 10⁻¹⁵ mol"

Intl rounds before it picks the exponent, so 999.96 Pa correctly becomes 1.00 kPa and not 1000 Pa.   is a non-breaking space, so a value never wraps onto a different line from its unit.

Why not Intl's unit style?

Intl.NumberFormat has style: 'unit', and it's great for everyday units: { style: 'unit', unit: 'celsius' } formats 21.5 as 21.5°C. But it only supports a fixed list of units, and most scientific ones aren't on it. unit: 'pascal' throws a RangeError. For anything beyond everyday units, appending the unit yourself is the practical option.

Lessons learned

  • Let Intl do the math. Rounding and exponent selection are easy to get subtly wrong by hand. formatToParts lets you change the look without redoing the math.
  • Precision is information. Set both minimum and maximum significant digits, so 1.20 doesn't become 1.2.
  • Decide on what users will see. Thresholds should check the rounded value, not the raw one.
  • Engineering notation maps to SI prefixes. Exponents in multiples of 3 turn into milli, micro, and kilo with a lookup table.
  • Handle the edges. Zero, NaN, Infinity, and values beyond the prefix range all need an answer.

Conclusion

Three small functions on top of Intl.NumberFormat cover most of what a scientific dashboard needs: readable exponents, honest precision, and proper SI units. They're plain JavaScript, so they work the same in a Vue template, a chart tooltip, or a CSV export.

Let me know if you found this useful. Feel free to reach out to me with any questions on LinkedIn.