How do I restrict a float value to only two places after the decimal point in C?

前端 未结 17 2739
孤城傲影
孤城傲影 2020-11-22 03:22

How can I round a float value (such as 37.777779) to two decimal places (37.78) in C?

17条回答
  •  别那么骄傲
    2020-11-22 03:49

    You can still use:

    float ceilf(float x); // don't forget #include  and link with -lm.
    

    example:

    float valueToRound = 37.777779;
    float roundedValue = ceilf(valueToRound * 100) / 100;
    

提交回复
热议问题