RepoPilot

Does this respect the user's locale for number formatting?

About sindresorhus/pretty-bytes · 2 messages · last activity 7/13/2026

Question

Does this respect the user's locale for number formatting?

RepoPilot

Yes, the library respects the user's locale for number formatting through the locale option. Here's how it works:

The toLocaleString function [index.js:L55-L65] handles locale-aware formatting:

const toLocaleString = (number, locale, options) => {
	let result = number;
	if (typeof locale === 'string' || Array.isArray(locale)) {
		result = number.toLocaleString(locale, options);
	} else if (locale === true || options !== undefined) {
		result = number.toLocaleString(undefined, options);
	}

	return result;
};

The locale option supports four modes [README]:

  • false (default): No localization
  • true: Uses the system/browser locale
  • string: A specific BCP 47 language tag (e.g., 'de', 'en')
  • string[]: A list of language tags to try in order

Test cases [test.js:L71-L110] demonstrate this in action. For example:

  • prettyBytes(1001, {locale: 'de'})'1 kB' (German formatting)
  • prettyBytes(1001, {locale: 'en'})'1 kB' (English formatting)
  • prettyBytes(1e30, {locale: 'de'})'1.000.000 YB' (German uses periods for thousands)
  • prettyBytes(1e30, {locale: 'en'})'1,000,000 YB' (English uses commas for thousands)

The main function applies locale options when formatting numbers [index.js:L124-L179], delegating to JavaScript's native Number.prototype.toLocaleString() for the actual formatting.

FOLLOW_UPS:

  • How does the fixedWidth option work?
  • What's the difference between binary and SI prefixes?
  • Can prettyBytes handle very large numbers?

Want to ask your own question?

Open chat for sindresorhus/pretty-bytes

Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/pretty-bytes. AI answers can be incomplete or stale — verify before relying on them.