gpt4 book ai didi

c# - 将字符串转换为十进制时出现 System.FormatException : Input string was not in a correct format ,。

转载 作者:太空狗 更新时间:2023-10-29 21:27:46 25 4
gpt4 key购买 nike

我在使用 ASP.NET 和 C# 时遇到了一点问题。这是我的错误代码:

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

}

我尝试了 Decimal.Parse、Decimal.TryParse 和其他奇怪的组合。现在我确定问题出在字符串并将它们解析为十进制。当我创建 String 变量时 - 解析时出现了同样的错误。那么有人可以告诉我如何将字符串转换为十进制吗?

最佳答案

尝试使用“0,85”而不是“0.85”。如果你改变文化,我认为你可以使用点小数。以下是更多信息: How to change symbol for decimal point in double.ToString()?

关于c# - 将字符串转换为十进制时出现 System.FormatException : Input string was not in a correct format ,。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com