Does this respect the user's locale for number formatting?
Does this respect the user's locale for number formatting?
About sindresorhus/pretty-bytes · 2 messages · last activity 7/20/2026
Yes, pretty-bytes does respect the user's locale for number formatting through the locale option.
The library uses the toLocaleString function [index.js:L55-L65] to format numbers according to a specified locale. Here's how it works:
Locale option behaviors:
-
locale: false(default) — No localization; numbers are formatted without locale-specific formatting. -
locale: true— Uses the system/browser's default locale for formatting. -
locale: 'de'or other language tag — Formats using a specific BCP 47 language tag (e.g., German uses commas as decimal separators and periods as thousand separators). -
locale: ['unknown', 'de', 'en']— Accepts an array of language tags; the first supported locale is used.
Example from the tests [test.js:L71-L110]:
prettyBytes(1001, {locale: 'de'})→'1 kB'(German formatting)prettyBytes(1e30, {locale: 'de'})→'1.000.000 YB'(uses periods for thousands)prettyBytes(1e30, {locale: 'en'})→'1,000,000 YB'(uses commas for thousands)
The implementation calls Number.prototype.toLocaleString() internally, which handles all locale-specific number formatting conventions [index.js:L55-L65].
FOLLOW_UPS:
- What does the
signedoption do? - How does the
binaryprefix option work? - What is the
fixedWidthoption for?
Want to ask your own question?
Open chat for sindresorhus/pretty-bytesPublic 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.