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

前端 未结 5 1578
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

    Why not from the left? the only difference is to use simple division:

    printf("%2d", 1234/100); // you get 12
    

提交回复
热议问题