易百教程

输出浮点数值

在C语言中,如何输出浮点数值?

实现代码

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

int main(void)
{
    float  fp1 = 345.678f, fp2 = 1.234E6f;
    double fp3 = 234567898.0, fp4 = 11.22334455e-6;

    printf_s("%f  %+f  %-10.4f  %6.4f\n", fp1, fp2, fp1, fp2);
    printf_s("%e  %+E\n", fp1, fp2);
    printf_s("%f  %g  %#+f  %8.4f  %10.4g\n", fp3, fp3, fp3, fp3, fp4);
    system("pause");
    return 0;
}

执行上面示例代码,得到以下结果:

345.678009  +1234000.000000  345.6780    1234000.0000
3.456780e+02  +1.234000E+06
234567898.000000  2.34568e+08  +234567898.000000  234567898.0000   1.122e-05