How to keep one decimal place in floating point numbers in C language.

In C language, we can use the printf function to achieve the operation of keeping one decimal place.

The sample code is shown below:

#include <stdio.h>

int main() {
    float num = 3.14159;
    printf("保留一个小数: %.1f\n", num);

    return 0;
}

The output is:

保留一个小数: 3.1

In formatted string, use %.1f to display one decimal place. You can adjust the number of decimal places as needed.

广告
Closing in 10 seconds
bannerAds