![](/img/trans.png)
[英]System.FormatException: Input string was not in a correct format for decimal
[英]System.FormatException : Input string was not in a correct format ,on converting string to decimal.
我對ASP.NET和C#有一點問題。 這是我的錯誤代碼:
mscorlib.dll中出現“System.FormatException”類型的異常,但未在>用戶代碼中處理
附加信息:輸入字符串的格式不正確。
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變量時 - 解析時出現了同樣的錯誤。 那么有人可以告訴我該怎么做才能將String轉換為十進制?
嘗試使用“0,85”而不是“0.85”。 如果你改變文化,我想你可以使用點小數。 以下是一些更多信息: 如何在double.ToString()中更改小數點的符號?
請嘗試使用以下代碼段。
CultureInfo info = CultureInfo.GetCultureInfo("es-ES");
string storedValue = "3,85";
decimal oldAmount;
bool succes = Decimal.TryParse(storedValue, NumberStyles.Any, info, out oldAmount);
請改用TextBox.Text:
bool succes = Decimal.TryParse(TextBox.Text, out oldAmount);
TextBox.value是錯誤的。 YourTextBox.Text是正確的......!
bool success = Decimal.TryParse(TextBox.Text, out oldAmount);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.