【问题标题】:Adding percentage to a number将百分比添加到数字
【发布时间】:2019-09-28 21:23:22
【问题描述】:

我正在尝试为某个数字加税,但在编译后,该税不包含在该数字的加法中。

我也尝试过 int 或 float 这个 %f 但编译后似乎没有进行数学运算。

#include <stdio.h>

int main() 
{

 float setprice, price, tax;

 printf("enter price in dollars : \n");
 scanf("%f", &price);


 printf("Enter the tax: \n");
 scanf("%f", &tax);

 setprice=(price*tax)+price;
 printf("total = %f\n", price);



    return 0;
}

在 IDE 上输入代码后,这是输出。

输入美元价格:
100
输入税金:
50
总计 = 100.000000

【问题讨论】:

  • 欢迎来到 Stackoverflow。我不知道 C#,但这看起来确实像 C 或 C++。请相应地使用正确的标签。
  • 我也将关闭标记为错字,因为你写了printf("total = %f\n", price);,你可能指的是printf("total = %f\n", setprice);
  • @uneven_mark 嘿,谢谢伙计,我能够解决这个问题。所以我所做的只是修改 setprice=(price+tax); printf("总计 = %f\n", setprice); .现在我能够计算税收和数量,并且输出没有错误。谢谢你,我一定会分配正确的标签。
  • 我不知道 C# 中的 printf() 函数。这是 C 还是 C++?
  • printf("total = %f\n", price); 而不是 printf("total = %f\n", setprice); 是那些 ...doah.... 时刻之一——我们都去过那里。 How to debug small programs 帮助。

标签: c


【解决方案1】:

您的问题是将"%" 添加到税收变量中。当你输入 50 时,它应该是 50%。

然后这样做

tax = tax / 100
setprice = (price * tax) + price;
printf("total = %f\n", setprice);

或者你可以试试这个

setprice = (price * (tax / 100)) + price;
printf("total = %f\n", setprice);

这些答案会奏效!

【讨论】:

    猜你喜欢
    • 2019-10-13
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多