在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) …Run Code Online (Sandbox Code Playgroud)