【问题标题】:Converting string/int to double in C#在 C# 中将 string/int 转换为 double
【发布时间】:2015-11-18 12:42:18
【问题描述】:

这是一个简单的计算器,用来计算将公斤价格除以你分配给糖果的钱,你能得到多少糖果。

例如:“糖果每公斤 5 美元。我为糖果分配了 3.50 美元。我会使用这个程序来计算我得到的糖果数量。”

在使用小数之前它可以正常工作。我想将字符串转换为 Double 以便我可以在计算器中使用小数。 4 美元的价格和 8 美元的钱当然会产生 2 公斤的糖果。但是如果公斤价格是 4.80 美元而我有 9.30 美元的钱,我只会得到一个错误并导致命令提示符崩溃。

static void Main(string[] args)
    {
      //The variables
        int int1;
        int int2;
        string text1;
        string text2;
      //The actual calculation
        System.Console.Write("Please input the kilogram price of candy: ");
        text1 = System.Console.ReadLine();
        int1 = int.Parse(text1);
        System.Console.Write("Please input the money allocated for candy ");
        text2 = System.Console.ReadLine();
        int2 = int.Parse(text2);
        System.Console.WriteLine("With the amount of money you input you would get " + (int2 / int1) + " kilos of candy.");
    }
}

}

【问题讨论】:

  • 简单地使用Double.Parse?! (虽然我建议使用 .TryParse)
  • 使用Double.TryParse(text1, out myDoubleVariable); 而不是使用整数。根据定义,整数是整数。
  • 为什么int 不是double?附言。 int1 是一个糟糕的变量名:p
  • 您的问题是“为什么我不能将4.80 存储在int 中”?也许您需要了解various numeric variable types C# offers
  • 另外,你没有甚至提供你的错误信息。请阅读FAQHow to Ask 几次..

标签: c# int double


【解决方案1】:

您正在使用int 变量类型,它只能包含integer numbers,即不带小数的“整数”数字,例如142。只要你想使用小数,你就需要一个可以支持它的变量类型。

C# 有一些内置的:floatdoubledecimal。鉴于您使用的是货币数据,其中精度和准确性非常重要,您必须使用 decimal

您还想使用 bool-returning TryParse() 而不是抛出异常的 Parse() 方法。

所以更改您的代码以使用decimaldecimal.TryParse()

decimal pricePerKilogram;
string input = Console.ReadLine();

if (!decimal.TryParse(input, out pricePerKilogram))
{
    // show error or retry
}

【讨论】:

  • 小补充,最好在TryParse中使用IFormatProvider(如InvariantCulture)来指定这个.NumberDecimalSeparator
  • 这个答案是最全面的,对我有帮助。我显然是 C# 的初学者,不知何故我还不能同化变量类型。当我开始使用这种语言时,我相信我将来会这样做。谢谢! @CodeCaster
【解决方案2】:

您需要解析来自decimal 的输入,而不是int。来,试试这个方法:

System.Console.Write("Please input the kilogram price of candy: ");
decimal pricePerKilo = decimal.Parse(System.Console.ReadLine());
System.Console.Write("Please input the money allocated for candy ");
decimal amountAllocatedForCandy = decimal.Parse(System.Console.ReadLine());
System.Console.WriteLine("With the amount of money you input you would get " + (amountAllocatedForCandy / pricePerKilo) + " kilos of candy.");
System.Console.Read();

int 是一种只能存储整数的类型 - 即没有小数部分/小数位 - 这就是为什么当您尝试将带有小数位的字符串数字解析为 int 时出错的原因.另一方面,decimal 用于存储带有小数部分的数字。

每种类型都有其用途:例如,您永远不想使用decimal 存储糖果块的数量,因为它只能是整数。这样做可能会给未来的代码维护者带来不必要的混乱,并可能导致难以发现的错误。

【讨论】:

  • 一般情况下我觉得var几乎无处不在,但是当OP明显不知道最基本的数据类型之间的区别时,你可能不应该使用它。跨度>
【解决方案3】:

那是因为 4.80 或 9.30 不是整数。

   double double1;
    double double2;
    string text1;
    string text2;
  //The actual calculation
    System.Console.Write("Please input the kilogram price of candy: ");
    text1 = System.Console.ReadLine();
    double1 = double.Parse(text1);
    System.Console.Write("Please input the money allocated for candy ");
    text2 = System.Console.ReadLine();
    double2 = double.Parse(text2);
    System.Console.WriteLine("With the amount of money you input you would get " + (double2 / double1) + " kilos of candy.");

注意:INT 存储整数值,如 1 2 3 4 等...

【讨论】:

  • 不要将double 用于货币数据。
  • 你的回答是非常糟糕的建议!切勿将double 用于需要精确的内容!
【解决方案4】:

关于你的问题:

首先使用doubles decimals

decimal price;
decimal allowance;

另外,一些额外的不请自来的建议:

这里有一些问题。

首先,您需要在WriteLine 语句中交换除法以获得正确的计算。

另外,我建议您将变量重命名为更具描述性的名称。

这是您的代码,经过一些修改。我已经评论了每个更改。

//The variables
    decimal price; //CHANGE: descriptive variables!
    decimal allowance;
    string priceInput;
    string allowanceInput;
//The actual calculation
    System.Console.Write("Please input the kilogram price of candy: ");
    priceInput = System.Console.ReadLine();
    price = decimal.Parse(priceInput); //CHANGE: use double.Parse here instead of int.Parse
    System.Console.Write("Please input the money allocated for candy ");
    allowanceInput = System.Console.ReadLine();
    allowance = decimal.Parse(allowanceInput); //CHANGE: use double.Parse here instead of int.Parse
    System.Console.WriteLine("With the amount of money you input you would get "
        + Math.Round(allowance / price, 2) + " kilos of candy.");
    //CHANGE: Divide the money you have by the amount it costs to get the KG of candy.

编辑:由于使用金钱而添加了小数。

【讨论】:

  • 您的编辑没有帮助。它没有解释为什么。最好将其排除在外并删除对双精度的所有引用,而是使用小数。
猜你喜欢
  • 2012-06-01
  • 2020-04-05
  • 2012-01-25
  • 2010-10-31
  • 2016-07-24
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多