我需要将下面的语句转换成
x = 123456.789;
并打印小数点前3位和小数点后2位的浮点型变量x。
输出
456.78
我尝试过的 printf("%6.2f",x);
最佳答案
floating point variable x with 3 digits before the decimal point and two digits after the decimal point.
你需要的是浮点取模余数函数fmod
double x = 123456.789;
double y = fmod( x, 1000 );
printf("%06.2f",y);
记住 #include <math.h>
,并为数学库使用 -lm
进行编译,示例:
gcc test.c -o test -lm
关于c - c中的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40965728/