문제

I have a question - it may seem stupid to most, but i'm still a novice coder. How does one round a floating variable to display upto only 2 or 3 digits of precision ? thanks in advance

도움이 되었습니까?

해결책

Use the format specifier %.2f for 2 digits of precision. Similarly use %.3f for 3 digits. For future reference, here are the printf format specifiers.

#include <stdio.h>

int main()
{
    printf("%.2f\n", 0.005); // prints 0.01
    printf("%.2f\n", 0.004); // prints 0.00
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top