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

前端 未结 17 3052
孤城傲影
孤城傲影 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:46

    printf("%.2f", 37.777779);
    

    If you want to write to C-string:

    char number[24]; // dummy size, you should take care of the size!
    sprintf(number, "%.2f", 37.777779);
    

提交回复
热议问题