LinuxCommandLibrary

numfmt

Format numbers to human-readable strings

TLDR

Convert 1.5K (SI Units) to 1500

$ numfmt --from si 1.5K
copy

Convert 5th field (1-indexed) to IEC Units without converting header
$ ls -l | numfmt --header=1 --field 5 --to iec
copy

Convert to IEC units, pad with 5 characters, left aligned
$ du [[-s|--summarize]] * | numfmt --to iec --format "%-5f"
copy

SYNOPSIS

numfmt [OPTION]... [NUMBER]...

PARAMETERS

--debug
    Print warnings about invalid input.

-d, --delimiter=X
    Use X instead of whitespace for field delimiter.

--field=N
    Replace the number in input field N; see FIELDS below.

--format=FORMAT
    Use printf style floating-point FORMAT; see FORMAT below. To enable use of the command line parameter, a single %f is required.

--from=WORD
    Auto-scale input numbers to WORD; see WORD below. Default is 'none'.

--from-unit=N
    Specify the input unit size (instead of the default 1).

--grouping
    Use locale-defined grouping separator, e.g., 1,000,000 (which has no effect when the locale does not define grouping).

--header[=N]
    Print first N header lines unchanged; N defaults to 1 if not specified.

--invalid=MODE
    Failure mode for invalid input: MODE can be: abort (default), fail, warn, ignore.

--padding=N
    Pad the output to N characters; positive N will right-align; negative N will left-align; padding is ignored if the output is wider than N; the default is to pad if a whitespace is found.

--round=METHOD
    Use METHOD for rounding when scaling; METHOD can be: up, down, from-zero (default), towards-zero, nearest.

--suffix=SUFFIX
    Add SUFFIX to output numbers, and accept optional SUFFIX in input numbers.

--to=WORD
    Auto-scale output numbers to WORD; see WORD below.

--to-unit=N
    The output unit size (instead of the default 1).

--version
    Output version information and exit.

--help
    Display this help and exit.

DESCRIPTION

The numfmt command converts numbers from/to human-readable formats. It can automatically scale numbers by adding suffixes like 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' for thousands, millions, billions, etc., or remove them when converting from a human-readable format. This is particularly useful for displaying file sizes, memory usage, or other large numerical values in a more user-friendly manner.
The command reads numbers from standard input, one number per line (unless otherwise specified), and outputs the formatted numbers to standard output. Options control aspects like rounding, padding, field width, and the use of SI or IEC prefixes.
numfmt supports various formatting options to tailor the output to specific requirements, making it a versatile tool for command-line scripting and data manipulation.

CAVEATS

numfmt relies on the locale settings for grouping separators. Results might vary depending on locale settings. Large numbers can cause unexpected behavior due to integer overflow.

WORD VALUES FOR --FROM AND --TO OPTIONS

The WORD must be one of: none, auto, si, iec, iec-i
* auto: accept optional single/double digit suffix: 1K = 1000, 1Ki = 1024, 1M = 1000000
* si: accept optional single letter suffix: 1K = 1000, 1M = 1000000
* iec: accept optional single letter suffix: 1K = 1024, 1M = 1048576
* iec-i: accept optional double letter suffix: 1Ki = 1024, 1Mi = 1048576
none is the default. When in auto mode, if no suffix is present, then si is assumed.

FIELDS

Each input field is specified by a single number. The default is field 1; fields are interpreted as in cut(1).

FORMAT

The FORMAT must be suitable for printing one floating-point argument '%f'. Optional quote (') is allowed; a %f is required in order to enable use of the command line parameter.

SEE ALSO

printf(1)

Copied to clipboard