我正在創建POS(銷售點),並且在嘗試將價格「0.60
」轉換爲整數時遇到問題。在C#中將包含句點(。)的字符串轉換爲Int32時的問題
數據背景:數據源的所有數據都來自MySQL數據庫,我已經設置並連接了沒有問題。
價格存儲在一個文本框中,並被格式化爲「0.60
」,我相信這是它未被轉換的原因。我不斷收到下面的消息。
附加信息:輸入字符串格式不正確。
//Puts the Price Into a String.
string NewPrice = txtPrice.Text;
//Converts The Quantity In the TextBox field to a numbers.
Quantity = Convert.ToInt32(txtQuant.Text);
//Incorrect Format & Attempt One.
//Price = Convert.ToInt32(NewPrice); <--- Problem.
//Price = int.Parse(NewPrice);
// I've also tried this method below with two '0' inside the { } brackets.
// But Still No Luck.
Price = Convert.ToInt32(string.Format("{0.00}",txtPrice.Text)); // <--- Problem.
// Times Price & Quantity to get Total Price (0.60 * 2 = 1.20)
TotalSingleItemPrice = Price * Quantity;
// The Single Item Price is added into the overall total.
TotalPrice += TotalSingleItemPrice;
// Converts Total Item Price to String from Int.
string TotalPriceStr = Convert.ToString(TotalSingleItemPrice);
// Puts TextBoxes/Strings Into One String array (I think).
string[] InizialItemList = new string[] { cmboInitItem.Text, Convert.ToString(Price), Convert.ToString(Quantity), TotalPriceStr};
// Adds The String Array Into the Data Grid View.
DGVIIL.Rows.Add(InizialItemList);
我試圖使用string.Format("{0.00}",txtPrice.Text)
設置來解決這個問題,我看不出有什麼我都過來看了。如果可能,我希望價格出現在我的DataGridView - DGVIIL中,如0.60
。
0.60不是整數... –
一個整數,不能包含小數點,你應該轉換爲浮動或十進制 –
@TMcKeown我知道這不是一個整數。有沒有合理的方法來解決這個問題?你可以用Float或Decimal做一個例子嗎?先謝謝了。 –