MCPcopy Create free account
hub / github.com/observablehq/framework / formatByteSize

Function formatByteSize

src/format.ts:17–29  ·  view source on GitHub ↗
(x: number, locale: Intl.LocalesArgument = "en-US")

Source from the content-addressed store, hash-verified

15}
16
17export function formatByteSize(x: number, locale: Intl.LocalesArgument = "en-US"): string {
18 const formatOptions = {maximumSignificantDigits: 3, maximumFractionDigits: 2};
19 for (const [k, suffix] of [
20 [1e9, " GB"],
21 [1e6, " MB"],
22 [1e3, " kB"]
23 ] as const) {
24 if (Math.round((x / k) * 1e3) >= 1e3) {
25 return (x / k).toLocaleString(locale, formatOptions) + suffix;
26 }
27 }
28 return x.toLocaleString(locale, formatOptions) + " B";
29}

Callers 3

format-test.tsFile · 0.85
loadFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected