Skip to content
Latchkey

numfmt: Human-Readable Sizes and Numbers

numfmt --to=iec turns a byte count into a human string like 4.7M, and --from reverses it.

Bundle-size and artifact-size gates read cleaner when numbers are formatted. numfmt converts both directions so a threshold check stays readable.

What it does

numfmt reformats numbers between plain integers and human units. --to=iec uses 1024-based units (K, M, G as KiB/MiB), --to=si uses 1000-based, and --from=iec/--from=si parses a human string back to an integer for arithmetic. Part of coreutils.

Common usage

Terminal
# bytes -> human (1024-based)
stat -c %s artifact.tar | numfmt --to=iec       # e.g. 4.7M
# human -> bytes for a comparison
numfmt --from=iec 4.7M                            # 4928307
# format a column of a report
du -b * | numfmt --to=iec --field=1
# fail a build if a bundle exceeds 500K
[ "$(stat -c %s dist/bundle.js)" -gt "$(numfmt --from=iec 500K)" ] && exit 1

Options

FlagWhat it does
--to=iec|si|iec-iFormat output using the given unit system
--from=iec|si|autoParse input as human-readable
--field=<n>Only convert field n of each line
--suffix=<s>Append/require a suffix such as B
--padding=<n>Pad the output to n columns
--round=<method>Rounding: up, down, nearest, etc.

In CI

Keep numeric comparisons in raw bytes and only format for the log line. Compute the threshold once with --from=iec, compare integers, and print numfmt --to=iec values in the failure message so the log reads "bundle 612K exceeds limit 500K".

Common errors in CI

"numfmt: invalid number: 4.7M" when converting a human string means you forgot --from (numfmt defaults to plain integers). "numfmt: invalid suffix in input" means the unit letter is not recognized for the chosen system (--from=si rejects Ki; use --from=iec or --from=auto). On Alpine, install coreutils; BusyBox has no numfmt.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →