printf, how to insert decimal point for integer

后端 未结 4 1351
一整个雨季
一整个雨季 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:37

    %.2d could add the extra padding zeros

    printf("%d.%.2d", n / 100, n % 100);
    

    For example, if n is 560, the output is: 5.60

    EDIT : I didn't notice it's UINT16 at first, according to @Eric Postpischil's comment, it's better to use:

    printf("%d.%.2d", (int) (x/100), (int) (x%100));
    

提交回复
热议问题