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

前端 未结 5 1584
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:15

    If you want to truncate from the right you can convert your number to a string and then use the string field width specifier.

    "%.3s".format(1234567.toString)
    

提交回复
热议问题