作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在使用 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/
我是一名优秀的程序员,十分优秀!