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

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

    There isn't a way to round a float to another float because the rounded float may not be representable (a limitation of floating-point numbers). For instance, say you round 37.777779 to 37.78, but the nearest representable number is 37.781.

    However, you can "round" a float by using a format string function.

提交回复
热议问题