小编non*_*eah的帖子

为什么我只有1%的时间得到不同的结果?

我试图截断超过百分之十的小数.(13.4396将是13.43)不是总是或从不工作?

我有这个99%的公式:

    input = (( (double) ((int)(orgInput * 100)) )  / 100) ;
Run Code Online (Sandbox Code Playgroud)

然后我有这个算术等价的公式序列:

    input = (orgInput * 100);
    input = (int) input;
    input = (double) (input);
    input = input / 100;
Run Code Online (Sandbox Code Playgroud)

这些输入不会产生所需的结果(公式截断并向下舍入,而细分步骤输出所需的结果):

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

c double int casting formula

3
推荐指数
1
解决办法
120
查看次数

标签 统计

c ×1

casting ×1

double ×1

formula ×1

int ×1