printf %f with only 2 numbers after the decimal point?

前端 未结 8 1557
温柔的废话
温柔的废话 2020-11-28 14:14

In my printf, I need to use %f but I\'m not sure how to truncate to 2 decimal places:

Example: getting

3.14159

8条回答
  •  渐次进展
    2020-11-28 14:50

    Try:

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

    Reference:

    http://www.cplusplus.com/reference/clibrary/cstdio/printf/

提交回复
热议问题