我正在尝试截断小数点后的所有内容。(13.4396 将是 13.43)它不是总是或从不工作吗?
我有这个 99% 有效的公式:
input = (( (double) ((int)(orgInput * 100)) ) / 100) ;
然后我有这个算术等价的公式序列:
input = (orgInput * 100);
input = (int) input;
input = (double) (input);
input = input / 100;
这些输入没有给出期望的结果(公式截断并向下舍入,而分解的步骤输出期望的结果):
12.33
99.99
22.22
44.32
56.78
11.11
然后对我来说更大的谜团是为什么 33.30 对它们中的任何一个都不起作用。下面是我的程序,它不断运行以演示我的问题。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double orgInput, input;
while (4!=3)
{
printf("Please enter the dollar amount (up to $100) => ");
scanf("%lf", &orgInput);
/* Truncates anything past hundredths place */
input = (orgInput * 100) ;
printf("\n1. input is %g \n ", input);
input = (int) input ;
printf("\n2. input is %g \n ", input);
input = (double) (input) ;
printf("\n3. input is %g \n", input);
input = input / 100 ;
printf("\n4. input is %.2lf \n", input);
input = (( (double) ((int)(orgInput * 100)) ) / 100) ;
printf("\nUsing the formula the input is %.2lf \n \n\n", input);
}
system("PAUSE");
return 0;
}
先感谢您!!!!PS对不起我仍然习惯于stackoverflow的格式
关键词:Double to int 转换精度误差