C# Decimal.Parse 和无效的输入字符串

C# Decimal.Parse and invalid input string

我遇到错误问题:输入字符串的格式不正确。我正在尝试在数据网格中转换货币。在出现错误的地方(这是我将值设置为值变量的地方)文本变量的值为 22.22,所以我不知道格式有什么问题。

public void valute()
{
    int rowCount = dataGridView1.RowCount;
    decimal value = 0;
    for (int i = 0; i < rowCount; i++)
    {
        string text = dataGridView1.Rows[i].Cells[3].Value.ToString();

        if (evro_check.Checked)
            dataGridView1.Rows[i].Cells[3].Value = text + " €";
        else if (dolar_check.Checked)
        {
            value = Decimal.Parse(text.Replace(',', '.'), CultureInfo.InvariantCulture);
            dataGridView1.Rows[i].Cells[3].Value = value.ToString() + " $";
        }
        else
        {
            dataGridView1.Rows[i].Cells[3].Value = value + " £";
        }
    }
}

编辑:现在我只是添加货币符号,稍后我也会将 € 更改为 $,这就是我使用附加变量(值)而不是对其他 2 种货币使用文本的方式。

注意文化。 例如,在英国,这个“10,000.10”是您的货币的 10,000 的 1/10,而在德国,格式是相反的:“10.000,10”。

您所做的是用“.”替换所有“,”。除非您将应用程序的当前文化更改为有意义的格式,否则这显然会以 FormatException 结束。

您应该将 CultureInfo 设置为您所定位的文化。

https://msdn.microsoft.com/en-US/library/b28bx3bh%28v=vs.80%29.aspx https://msdn.microsoft.com/en-us/library/bz9tc508%28v=vs.140%29.aspx

此外,最好使用格式提供程序,它会根据指定的文化为您格式化正确的货币字符串:

decimal value = 10000;

value.ToString("C", CultureInfo.InvariantCulture);
// Output : ¤10,000.00

value.ToString("C", CultureInfo.GetCultureInfo("de-DE"));
// Output : 10.000,00 €

value.ToString("C", CultureInfo.GetCultureInfo("en-US")).Dump();
// Output: ,000.00

如果您注意到,美国格式将货币符号放在前面,而德国格式放在最后。你也没有考虑任何这些事情。

参见:https://msdn.microsoft.com/en-us/library/0b22a4x2%28v=vs.110%29.aspx

试试这个

public void valute()
{
    int rowCount = dataGridView1.RowCount;
    decimal value = 0;
    for (int i = 0; i < rowCount; i++)
    {
        string text = dataGridView1.Rows[i].Cells[3].Value.ToString();

        if (evro_check.Checked)
            dataGridView1.Rows[i].Cells[3].Value = text + " €";
        else if (dolar_check.Checked)
        {
            if (text != "" || text != "&nbsp;")
            {
                value = Decimal.Parse(text.Replace(',', '.'), CultureInfo.InvariantCulture);
                dataGridView1.Rows[i].Cells[3].Value = value.ToString() + " $";
            }
        }
        else
        {
            dataGridView1.Rows[i].Cells[3].Value = value + " £";
        }
    }
}

您最好的选择是使用 Tryparse 而不是 Parse

TryParse

This overload differs from the Decimal.Parse(String) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed numeric value. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.

改进代码的建议

string text = dataGridView1.Rows[i].Cells[3].Value.ToString();
Decimal value=0;

if (Decimal.TryParse(text.Replace(',', '.'), out value))
{
   //parse success
   dataGridView1.Rows[i].Cells[3].Value = value.ToString() + " $"; // put the correct value
}
else {
   //parse not success 
   dataGridView1.Rows[i].Cells[3].Value ="- $"; // Put something which allow you to identify the issue.
 }

这将使您能够识别数据网格中哪些地方的值格式错误。

你可以利用 Culture Info class.

  public void valute()
  {
   int rowCount = dataGridView1.RowCount;
   decimal value = 0;
   for (int i = 0; i < rowCount; i++)
   {
       string text = dataGridView1.Rows[i].Cells[3].Value.ToString();

       if (evro_check.Checked)
           dataGridView1.Rows[i].Cells[3].Value = text.ToString("C", CultureInfo.GetCultureInfo("fr")); //€
       else if (dolar_check.Checked)
       {
           value = Decimal.Parse(text.Replace(',', '.'), CultureInfo.InvariantCulture);
           dataGridView1.Rows[i].Cells[3].Value = text.ToString("C", CultureInfo.GetCultureInfo("fr-CA")); //$
       }
       else
       {
           dataGridView1.Rows[i].Cells[3].Value = text.ToString("C", CultureInfo.GetCultureInfo("en-GB"));
       }
   }
  }

第二种方法 使用 Unicode

public static char HexToChar(string hex)
{
  return (char)ushort.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}

\u00A3 是英镑符号,£

\u20AC 是欧元符号,€。

\u0024 是美元符号,$。

How to insert a Symbol (Pound, Euro, Copyright) into a Textbox