numfmt

numfmt converts numbers between plain digits and human-readable forms — turning 1500000 into 1.5M, or 1.5M back into 1500000.

numfmt [options] [number...]
$ numfmt --to=si 1500000
1.5M
$ numfmt --from=si 1.5M
1500000

numfmt takes numbers as arguments, or — with none — reads them from standard input, which lets it rescale a column of numbers flowing through a pipe.

The direction of conversion #

OptionEffect
--from=UNITParse input numbers that carry a UNIT suffix, scaling them up to plain digits.
--to=UNITScale output numbers down and add a UNIT suffix.

A UNIT is one of:

UnitSuffixes mean
noneNo suffixes — a suffix is an error. The default.
siPowers of 1000: 1K = 1000, 1M = 1000000.
iecPowers of 1024: 1K = 1024, 1M = 1048576.
iec-iPowers of 1024 with a two-letter suffix: 1Ki = 1024, 1Mi = 1048576.
autoAccept any of the above on input, reading Ki/Mi as powers of 1024 and bare K/M as powers of 1000.

Working on fields #

By default numfmt converts the whole input line. To convert numbers sitting in a column of wider text, name the field:

OptionEffect
--field=FIELDSConvert only the numbers in these fields. FIELDS uses the same range syntax as cut3, 2-5, 2-.
-d, --delimiter=XUse X to separate fields instead of whitespace.
--header[=N]Pass the first N lines through unconverted, as a header.

Shaping the output #

OptionEffect
--format=FORMATFormat the number with a printf-style floating-point FORMAT, controlling width, padding, and precision.
--padding=NPad the output to N columns — positive right-aligns, negative left-aligns.
--groupingGroup digits according to the locale (for example, 1,000,000).
--round=METHODChoose the rounding method used when scaling.
--suffix=SUFFIXAppend SUFFIX to each result, and accept it on input.
--from-unit=N / --to-unit=NSet the unit size the input or output is counted in.

Other options #

OptionEffect
--invalid=MODEChoose what to do with input that is not a valid number: abort, fail, warn, or ignore.
-z, --zero-terminatedTreat the NUL character as the line delimiter.
--debugPrint warnings about questionable input.

Exit status #

CodeMeaning
0Every number was converted.
2An input was not a valid number, or an option was misused.

Edit this page