How to cut decimal off without rounding in C

后端 未结 5 1987
无人共我
无人共我 2020-12-01 18:11

How do I cut off decimal places in C without rounding?

For example if the number is 4.48

it will just show 4.4

%.1f

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 19:04

    You can (ab)use the fact that integer division truncates and doesn't round:

    float original = 4.48;
    
    int tmp = original * 10; // 44.8 truncated to 44
    
    float truncated = tmp / 10.0; // 4.4
    

提交回复
热议问题