在 C# 中将 string/int 转换为 double

Converting string/int to double in C#

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

例如:"Candy costs 5$ a kilo. I have 3.50$ allocated for candy. I would use this program to calculate the amount of candy I get."

在使用小数之前它工作正常。我想将字符串转换为 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.");
    }
}

}

那是因为 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 等...

您需要解析来自 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 是一种只能存储整数的类型——即没有小数 component/decimal 数字——这就是为什么当你试图用小数解析字符串数字时出现错误的原因数字变成 int。另一方面,decimal 用于存储带小数部分的数字。

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

您正在使用 int 变量类型,它只能包含 integer numbers,即 "whole" 没有小数的数字,例如 142 .只要你想使用小数,你就需要一个可以支持它的变量类型。

C# 内置了一些:floatdoubledecimal。鉴于您正在处理货币数据,其中精度和准确性非常重要,因此您必须使用 decimal.

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

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

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

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

关于您的问题:

首先使用 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.

编辑:由于与金钱打交道而增加了小数点。