Specifying maximum printf field width for numbers (truncating if necessary)?

前端 未结 5 1562
Happy的楠姐
Happy的楠姐 2021-02-19 00:09

You can truncate strings with a printf field-width specifier:

printf(\"%.5s\", \"abcdefgh\");

> abcde

Unfortunately it does no

5条回答
  •  囚心锁ツ
    2021-02-19 00:37

    Example from Bash command line:

    localhost ~$ printf "%.3s\n" $(printf "%03d"  1234)
    123
    localhost ~$ 
    

提交回复
热议问题