2015-02-09 80 views
0

我手動複製excel到字符串集合(組合框)2列,1是帳戶(456939493)號碼第二個是百分比(0.001)的小數。C#組合框項目數乘法

this.percent.Items.AddRange(new object[] { 
     "456939493 0.001 ", 
     "453949343 0.00001", 

操作

double Pairdecimal = Convert.ToDouble(percent.SelectedValue); 

當執行乘法運算它不讀取小數,並且只有產生的數爲零。

如何才能從字符串集合(ComboBox)中獲取小數點而不是帳號。

回答

0

感謝您的回答! 我把這兩個建議/答案,並使其適用於我的代碼。

string[] str = currpair.Text.Split(); //from Ric Gaudet 

然後,我還拿了

double secondValue = Convert.ToDouble(str[1]); //from swistak 

再次感謝我的問題就解決了。 現在我可以乘以組合框的值。

1

您可以拆分字符串,然後將第一部分轉換爲int。像這樣:

var splitStrings = percent.SelectedValue.Split(); 
var firstValue = Convert.ToInt32(splitStrings[0]); //int 
var secondValue = Convert.ToDouble(splitStrings[1]); //double 
1

有很多方法可以做到這一點,swistak提供了一個很好的答案。 您需要首先將字符串分離爲其組成部分,然後將所需的部分轉換爲雙精度(或十進制)。

 string text = "456939493 0.001 "; 

     //one option 
     string[] textArray = text.Split(' '); 
     double num1 = Convert.ToDouble(textArray[1]); 

     //another option 
     double num2 = Convert.ToDouble(text.Substring(10)); 
     // this assumes the account number is always the same length