我如何在不对给定程序中的双精度值进行四舍五入的情况下保持正确的输出值

How do i hold the correct output value without roundoff the double value in given program

给定一顿饭的餐价(一顿饭的基本费用)、小费百分比(作为小费添加的餐费百分比)和税费百分比(作为税添加的餐费百分比) , 查找并打印这顿饭的总费用。

条件:请务必使用精确的值进行计算,否则您可能会得到不正确的四舍五入结果!

这是我的程序:

static void Main(String[] args)    
{

    double mealCost=Convert.ToDouble(Console.ReadLine());

    int tipPercent=Convert.ToInt32(Console.ReadLine());

    int taxPercent=Convert.ToInt32(Console.ReadLine());

    double tip,tax;

    tip=(mealCost*(tipPercent/100));

    tax=(mealCost*(taxPercent/100));

    double totalCost=mealCost+tip+tax;

    Console.WriteLine("The total meal cost is {0} dollars",totalCost);

    Console.ReadLine();

}

但我的输出是 12。

我的预期输出是 15。

如果我的示例输入是 12.00 20 8

我的计算结果为 tip=2.4tax=0.96, totalCost=15.36 舍入后的值为 (round)totalCost=15.

但是输出结果是 12。

如何在 C# 中获得正确的输出。谁能给我一些解决这个问题的建议。

您需要做的是,将 tipPercenttaxPercent 作为双精度值,或者在处理除法之前将它们隐式转换为双精度值,如下所示:

tip = (mealCost * ((double)tipPercent / 100));
tax = (mealCost * ((double)taxPercent / 100));

然后您将得到 totalCost=15.36 对于问题中指定的输入。更聪明的解决方案是:

 double mealCost, tipPercent, taxPercent;
 Console.WriteLine("Enter values for Meal Cost, Tip percentage and tax percentage");
 if (!double.TryParse(Console.ReadLine(), out mealCost))
 {
     Console.WriteLine("Invalid input for meal Cost");
 }
 if (!double.TryParse(Console.ReadLine(), out tipPercent))
 {
     Console.WriteLine("Invalid input for Tip percentage");
 }

 if (!double.TryParse(Console.ReadLine(), out taxPercent))
 {
     Console.WriteLine("Invalid input for Tip tax Percent");
 }
 double tip = (mealCost * (tipPercent / 100));
 double tax = (mealCost * (taxPercent / 100));
 double totalCost = mealCost + tip + tax;
 Console.WriteLine("The total meal cost is {0}", totalCost.ToString("C0"));

 Console.ReadLine();

您得到 0,因为您用代码将 int 值除以 int 值。除法将结果向零舍入,结果的绝对值是小于两个操作数的商的绝对值的最大可能整数。您可以查看 C# 如何处理它 here.

如果您重写您的小费,税金计算如下。

 tip = (mealCost * tipPercent / 100);
 tax = (mealCost * taxPercent / 100);

您的问题与这两行有关:

 tip=(mealCost*(tipPercent/100));

 tax=(mealCost*(taxPercent/100));

这些定义了整数和 return 整数之间的运算。像这样强制转换为 double 或将 100 声明为 double:100d

首先,请将您的数据类型改为decimal而不是double,更适合与金钱相关的。

其次,当你进行计算时,C#会尝试return具有相同的数据类型,这导致:

tip=(mealCost*(tipPercent/100));    // it will turn tipPercent/100 to int, which is 0
tax=(mealCost*(taxPercent/100));    // same here

你有很多方法可以做到,比如 cast as double:

tip = (mealCost * ((double) tipPercent / 100));

100 声明为 100D(告诉 c# 它是双精度的)

tip = (meanCost * (tipPercent / 100D));

或者,只需为您的 tipPercenttaxPercent

使用双精度/小数
double tipPercent = Convert.ToDouble(Console.ReadLine());

在除法中你应该至少有一个双倍的答案才能得到双倍的答案非常少你需要让它工作的变化见下文

static void Main(String[] args) 
{
double mealCost=Convert.ToDouble(Console.ReadLine());

int tipPercent=Convert.ToInt32(Console.ReadLine());

int taxPercent=Convert.ToInt32(Console.ReadLine());

double tip,tax;

tip=(mealCost*(tipPercent/100.0));//change 100 to 100.0

tax=(mealCost*(taxPercent/100.0));//change 100 to 100.0

double totalCost=mealCost+tip+tax;

Console.WriteLine("The total meal cost is {0} dollars",totalCost);

Console.ReadLine();

}