2013-01-01 21 views
4

我開發在asp.net網站,並有這樣的問題:減法整數值的字符串變量在asp.net

string price = row["Price"].ToString(); 
string discount = row["Discount"].ToString(); 

此代碼是從DataTable採集數據,而其正常工作。兩者都是浮點值(12,50.75 ...)。在我的程序中,我想從「價格」中減去「折扣」並將結果分配給一個新的字符串。假設字符串價格包含50並且字符串折扣包含23.5,那麼我需要26.5在新字符串中。我該如何做到這一點?

+0

你爲什麼不轉換值浮動?然後做數學,然後將其轉換回字符串... – AMember

+0

謝謝。讓我知道如何將它轉換爲float.Visual工作室不顯示一個選項。 – samiaj

回答

1

你行你下面的代碼:

string strPrice = row["Price"].ToString(); 
    string strDiscount =row["Discount"].ToString(); 
    string strFinal = (Convert.ToDouble(strPrice) - Convert.ToDouble(strDiscount)).ToString(); 
0

您需要double.Parse使它們編號並對它們執行算術運算。

double price = double.Parse(row["Price"].ToString()); 
double discount = double.Parse(row["Discount"].ToString()); 

double amount = price - discount; 
+0

您需要在第二行花花公子Parse後領先私人分配。 –

+0

謝謝..他爲我工作 – samiaj

0
string newPrice = (double.Parse(price)-double.Parse(discount)).ToString(); 
0
string strDiff = (Convert.ToDouble(row["Price"].ToString()) - Convert.ToDouble(row["Discount"].ToString())).ToString(); 
0

是的,你可以像這樣

double amount=(convert.toDouble(row["price"].tostring()))-(convert.toDouble(row["Discount"].tostring())); 

string Totalamount=amount.tostring(); 
0

你可以轉換雙字符串變量採用一倍Convert.ToDouble或使用double.Parse()函數。 這兩個check

double price ,discount,amount; 
string result = null; 
price = double.Parse(row["Price"].ToString()); 
discount = double.Parserow["Discount"].ToString()); 
amount = price - discount; 
string result = Convert.ToString(amount); 
0

之間差異試試這個..

double amount=(convert.todouble(row["price"].tostring()))-(convert.todouble(row["Discount"].tostring()));