qua*_*ore 0 c floating-point gcc
在Ubuntu Linux 10.04上使用GCC,我在分割后进行了不必要的舍入.
我试过了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FormatReading(int temp)
{
double reading = temp / 100;
printf("%f\n",reading); /* displays 226.000000, was expecting 226.60 */
}
int main(void)
{
FormatReading(22660);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人建议我试试:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FormatReading(int temp)
{
long reading = temp ;
reading = reading / 100;
printf("%3.2ld\n",reading); /* displays 226 */
}
int main(void)
{
FormatReading(22660);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FormatReading(int temp)
{
long reading = temp ;
double reading2 = reading / 100;
printf("%3.2f\n",reading2); /* displays 226.00 */
}
int main(void)
{
FormatReading(22660);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用include math.h和编译器标签-lm以各种方式使用round函数,但是没有找到我想要的东西.
任何帮助非常感谢.
此致,伯特
double reading = temp / 100.0;
^^
Run Code Online (Sandbox Code Playgroud)
temp / 100 是整数除法 - 您将结果分配给double不会更改此值.