【问题标题】:float not working in if else statement (c)浮动在 if else 语句中不起作用(c)
【发布时间】:2018-12-17 15:33:37
【问题描述】:

每当我尝试在这个IF ELSE statement 中使用浮点值时,它都会给我0 或不正确的答案。我在其余代码中使用了INT,其余的工作正常。这是我第一次在代码中使用浮点数,现在它没有给我想要的答案。

你能告诉我我做错了什么吗?

float interestrate;

    if (month < 49)
    {
        interestrate = (0.063);
    }
    else if (salary <= 25000)
    {
        interestrate = (0.033);
    }
    else if (salary > 45000)
    {
        interestrate = (0.063);
    }
    else
    {
        interestrate = (0.033+(salaryabovethreshold*0.0000015));
    }

    printf("Interest Rate: %d \n", interestrate);

【问题讨论】:

  • “不工作”的是printf。检查您用于打印的格式说明符floats
  • 嗯,好的。我应该使用其他东西来代替 printf 吗?
  • 您应该使用%f 而不是%d。建议在printf上阅读documentation
  • 这些东西 %d%f 等(格式说明符)通常在初学者级 C 编程书籍的第 1 章中讨论。

标签: c if-statement int


【解决方案1】:

%d 格式说明符用于ints,而不是floats。对于浮点类型,请尝试使用 %f%e%g%a

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2023-03-13
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多