繁体   English   中英

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

[英]C# Decimal.Parse and invalid input string

我遇到错误问题:输入字符串的格式不正确。 我正在尝试在datagrid中转换货币。 在出现错误的地方(这是我将值设置为value变量的位置),文本变量的值为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种货币。

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

TryParse

此重载与Decimal.Parse(String)方法不同,它返回一个布尔值,该值指示解析操作是否成功,而不是返回解析后的数值。 如果s无效且无法成功解析,则无需使用异常处理来测试FormatException。

改进代码的建议

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.
 }

这将使您能够确定数据网格中值格式错误的位置。

注意文化。 例如,在英国,“ 10,000.10”是您货币的1万和1/10,而在德国,格式是“ 10.000,10”。

您要做的是将所有“,”替换为“。”。 除非您将应用程序的当前区域性更改为有意义的格式,否则显然这将以FormatException结尾。

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

https://msdn.microsoft.com/zh-CN/library/b28bx3bh%28v=vs.80%29.aspx https://msdn.microsoft.com/zh-CN/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: $10,000.00

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

请参阅: https : //msdn.microsoft.com/zh-cn/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 + " £";
        }
    }
}

您可以使用Culture Info类

  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是美元符号$。

如何在文本框中插入符号(英镑,欧元,版权)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM