printf, how to insert decimal point for integer

后端 未结 4 1384
一整个雨季
一整个雨季 2021-01-19 11:14

I have an UINT16 unsigned integer of say

4455, 312, 560 or 70.

How to use printf to insert a decimal point before the last two

4条回答
  •  独厮守ぢ
    2021-01-19 11:48

    You can use printf directly with out using float

    printf("%d.%02d", num/100, num%100);
    

    %02d means right align with zero padding.

    if num is 4455 ==>output is 44.55  
    if num is 203  ==>output is 2.03
    

    EDIT:
    by seeing comment from @ Eric Postpischil , it's better to use like this.

    printf("%d.%02d", (int) (num/100), (int) (num%100));
    

提交回复
热议问题