国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > numfmt 以获得更好的体验。

# Bash 的 numfmt 命令

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

!subtitle:功能

数值和字符串之间的转换。

!subtitle:类型

可执行文件(/usr/bin/numfmt),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • --debug - 对无效输入打印警告

    • -d, --delimiter=X - 使用 X 代替空格作为分隔符

    • --field=FIELDS - 替换 FIELDS 指定的 字段)(默认为 1)中的数字

    • --format=FORMAT - 使用 printf 风格的浮点数格式

    • --from=UNIT - 指定输入的 单位格式) 为 UNIT

    • --from-unit=N - 指定输入的单位大小为 N;使用此选项但不指定 N 时默认为 1

    • --grouping - 使用区域定义的数值分组;例如 1,000,000

    • --header[=N] - 开头 N 行直接打印,不转换;默认为 1

    • --invalid=MODE - 无效数值的处理模式:

      • abort - 中断(默认)

      • fail - 失败

      • warn - 警告

      • ignore - 忽略

    • --padding=N - 将输出对齐到 N 个字符

    • --round=METHOD - 舍入方式:

      • up - 向上取整

      • down - 向下取整

      • from-zero - 从零远离(默认)

      • towards-zero - 向零靠拢

      • nearest - 最近整数

    • --suffix=SUFFIX - 给输出添加后缀 SUFFIX

    • --to=UNIT - 指定输出的 单位格式) 为 UNIT

    • --to-unit=N - 指定输入的单位大小为 N;默认为 1

    • -z, --zero-terminated - 以空字符(\0)作为行的结尾,而不是换行符(\n

    • --ignore=N - 忽略 N 个逻辑核心

    • --help - 显示帮助

    • --version - 显示版本

  • NUMBER - 要转换的数值

# 单位格式

单位格式 说明
none 没有单位,后缀会产生错误
auto 1K = 1000, 1Ki = 1024
si 1K = 1000
iec 1K = 1024
iec-i 1Ki = 1024

# 字段

字段 说明
N 第 N 个字段
N- 第 N 个字段到末尾
N-M 第 N 个字段到第 M 个字段
-M 从开始到第 M 个字段
- 所有字段
  • 字段的开始序号是 1

# 示例

$ numfmt --to=si 100000000
100M
$ numfmt --to=iec 100000000
96M
$ numfmt --from=si 32G
32000000000
$ $ numfmt --from=si 32G
32000000000
$ numfmt --to=iec --from=si 1T      # 1T 硬盘的实际容量
932G

# 推荐阅读

# 手册

NUMFMT(1)                        User Commands                       NUMFMT(1)

NAME
       numfmt - Convert numbers from/to human-readable strings

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

DESCRIPTION
       Reformat  NUMBER(s),  or  the  numbers  from standard input if none are
       specified.

       Mandatory arguments to long options are  mandatory  for  short  options
       too.

       --debug
              print warnings about invalid input

       -d, --delimiter=X
              use X instead of whitespace for field delimiter

       --field=FIELDS
              replace  the  numbers  in  these  input  fields (default=1); see
              FIELDS below

       --format=FORMAT
              use printf style floating-point FORMAT; see FORMAT below for de‐
              tails

       --from=UNIT
              auto-scale input numbers to UNITs; default is 'none';  see  UNIT
              below

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

       --grouping
              use  locale-defined  grouping  of  digits, e.g. 1,000,000 (which
              means it has no effect in the C/POSIX locale)

       --header[=N]
              print (without converting) the first N header lines; N  defaults
              to 1 if not specified

       --invalid=MODE
              failure  mode for invalid numbers: 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 automatically pad  if  a  white‐
              space 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  in‐
              put numbers

       --to=UNIT
              auto-scale output numbers to UNITs; see UNIT below

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

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

   UNIT options:
       none   no auto-scaling is done; suffixes will trigger an error

       auto   accept optional single/two letter suffix:

              1K = 1000, 1Ki = 1024, 1M = 1000000, 1Mi = 1048576,

       si     accept optional single letter suffix:

              1K = 1000, 1M = 1000000, ...

       iec    accept optional single letter suffix:

              1K = 1024, 1M = 1048576, ...

       iec-i  accept optional two-letter suffix:

              1Ki = 1024, 1Mi = 1048576, ...

   FIELDS supports cut(1) style field ranges:
       N      N'th field, counted from 1

       N-     from N'th field, to end of line

       N-M    from N'th to M'th field (inclusive)

       -M     from first to M'th field (inclusive)

       -      all fields

       Multiple fields/ranges can be separated with commas

       FORMAT  must be suitable for printing one floating-point argument '%f'.
       Optional quote (%'f) will enable --grouping (if  supported  by  current
       locale).   Optional  width  value (%10f) will pad output. Optional zero
       (%010f) width will  zero  pad  the  number.  Optional  negative  values
       (%-10f)  will  left align.  Optional precision (%.1f) will override the
       input determined precision.

       Exit status is 0 if all input numbers were successfully converted.   By
       default,  numfmt will stop at the first conversion error with exit sta‐
       tus 2.  With --invalid='fail' a warning is printed for each  conversion
       error  and the exit status is 2.  With --invalid='warn' each conversion
       error is diagnosed, but the exit status is 0.  With  --invalid='ignore'
       conversion errors are not diagnosed and the exit status is 0.

EXAMPLES
              $ numfmt --to=si 1000

              -> "1.0K"

              $ numfmt --to=iec 2048

              -> "2.0K"

              $ numfmt --to=iec-i 4096

              -> "4.0Ki"

              $ echo 1K | numfmt --from=si

              -> "1000"

              $ echo 1K | numfmt --from=iec

              -> "1024"

              $ df -B1 | numfmt --header --field 2-4 --to=si
              $ ls -l  | numfmt --header --field 5 --to=iec
              $ ls -lh | numfmt --header --field 5 --from=iec --padding=10
              $ ls -lh | numfmt --header --field 5 --from=iec --format %10f

AUTHOR
       Written by Assaf Gordon.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/numfmt>
       or available locally via: info '(coreutils) numfmt invocation'

GNU coreutils 9.4                 April 2024                         NUMFMT(1)
本文 更新于: 2025-11-27 09:38:15 创建于: 2025-11-27 09:38:15