System.FormatException:输入字符串的不正确的格式,在将字符串转换为十进制。 [英] System.FormatException : Input string was not in a correct format ,on converting string to decimal.

查看:274
本文介绍了System.FormatException:输入字符串的不正确的格式,在将字符串转换为十进制。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET和C#的一个小问题。这是我的错误code:


  

型System.FormatException的异常出现在mscorlib.dll但在>用户code没有处理


  
  

其他信息:输入字符串的不正确的格式


 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(this.IsPostBack == FALSE)
    {
        Currency.Items.Add(新的ListItem(欧元,0.85));
        Currency.Items.Add(新的ListItem(言,11.30));
        Currency.Items.Add(新的ListItem(PLN,4.20));
        Currency.Items.Add(新的ListItem(英镑,5.62));
    }
}保护无效Convert_Click(对象发件人,EventArgs的发送)
{
    十进制oldAmount;
    布尔更迭= Decimal.TryParse(TextBox.Value,出oldAmount);    如果(更迭)
    {
        列表项项= Currency.Items [Currency.SelectedIndex]
        小数newAmount = oldAmount * decimal.Parse(item.Value);
        Result.InnerText =结果:+ newAmount;
    }}

我试过Decimal.Parse,Decimal.TryParse和其他奇怪的组合。现在,我敢肯定,问题是与串并分析它们为十进制。当我创建字符串变量 - 有是在解析同样的错误。因此,有人可以告诉我该怎么做才能将字符串转换为十进制?


解决方案

尝试使用0.85而不是0.85。我认为,如果你改变文化,你可以使用点分十进制。
下面是一些更多的信息:
<一href=\"http://stackoverflow.com/questions/3135569/how-to-change-symbol-for-decimal-point-in-double-tostring\">How在double.ToString()来改变符号小数点?

I have a little problem with ASP.NET and C#. This is my error code:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in >user code

Additional information: Input string was not in a correct format.

protected void Page_Load(object sender, EventArgs e)
{
    if(this.IsPostBack == false)
    {
        Currency.Items.Add(new ListItem("Euro", "0.85"));
        Currency.Items.Add(new ListItem("Yen", "11.30"));
        Currency.Items.Add(new ListItem("PLN", "4.20"));
        Currency.Items.Add(new ListItem("GBP", "5.62"));
    }
}

protected void Convert_Click(object sender, EventArgs e)
{
    decimal oldAmount;
    bool succes = Decimal.TryParse(TextBox.Value, out oldAmount);

    if(succes)
    {
        ListItem item = Currency.Items[Currency.SelectedIndex];
        decimal newAmount = oldAmount * decimal.Parse(item.Value);
        Result.InnerText = "Result: " + newAmount;
    }

}

I tried Decimal.Parse, Decimal.TryParse and other strange combinations. Now I'm sure that issue is with strings and parsing them to decimal. When I created String variable - there was that same error while parsing. So can someone tell me what to do to be able to convert String to decimal?

解决方案

Try using "0,85" instead of "0.85". I think you can use the dot decimal if you change culture. Here is some more information: How to change symbol for decimal point in double.ToString()?

这篇关于System.FormatException:输入字符串的不正确的格式,在将字符串转换为十进制。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆