numfmt

From RaySoft

numfmt reads numbers in various representations and reformats them as requested. The most common usage is converting numbers to/from human representation (e.g. 4G4,000,000,000).[1]

Documentation

  • man 1 'numfmt'

Syntax

numfmt [PARAMETER ...] [NUMBER]

Parameters

--from=UNIT
Auto-scales input numbers according to UNIT. See Units below. The default is no scaling, meaning suffixes (e.g. M, G) will trigger an error.
--grouping
Group digits in output numbers according to the current locale’s grouping rules.
--round=METHOD
When converting number representations, round the number according to METHOD, which can be up, down, from-zero (the default), towards-zero, nearest.
--suffix=SUFFIX
Add SUFFIX to the output numbers, and accept optional SUFFIX in input numbers.
--to=UNIT
Auto-scales output numbers according to UNIT. See Units below. The default is no scaling, meaning all the digits of the number are printed.
-z, --zero-terminated
Delimit items with a zero byte rather than a newline.

Units

The following are the possible unit options with --from=UNITS and --to=UNITS:

none
No scaling is performed. For input numbers, no suffixes are accepted, and any trailing characters following the number will trigger an error. For output numbers, all digits of the numbers will be printed.
si
Auto-scale numbers according to the International System of Units (SI) standard. For input numbers, accept one of the following suffixes. For output numbers, values larger than 1000 will be rounded, and printed with one of the following suffixes: K, M, G, T, P, E, Z, Y
iec
Auto-scale numbers according to the International Electrotechnical Commission (IEC) standard. For input numbers, accept one of the following suffixes. For output numbers, values larger than 1024 will be rounded, and printed with one of the following suffixes: K, M, G, T, P, E, Z, Y
iec-i
Auto-scale numbers according to the International Electrotechnical Commission (IEC) standard. For input numbers, accept one of the following suffixes. For output numbers, values larger than 1024 will be rounded, and printed with one of the following suffixes: Ki, Mi, Gi, Ti, Pi, Ei, Zi, Yi
auto

auto can only be used with --from. With this method, numbers with K, M, G, T, P, E, Z, Y suffixes are interpreted as si values, and numbers with ‘Ki, Mi, Gi, Ti, Pi, Ei, Zi, Yi suffixes are interpreted as iec values.

Examples

Scale to unit
numfmt --suffix='B' --to='iec' $((1024*1024))

Output:

1,0MB
Scale from units
LC_ALL='de_CH.utf8' numfmt --from='iec' --grouping --suffix='B' '1GB'

Output:

1’073’741’824B
Transform from SI to IEC
numfmt --from='iec' --suffix='B' --to='si' '1YB'

Output:

1,3YB

References