2013-03-15 47 views
1

如何在C#中將43961529(10%)轉換爲43,961,529.00在C#中將「43961529(10%)」轉換爲「43,961,529.00(10%)」?

我的價值43961529(10%)在一個DataTableDataColumn,我想將其轉換爲43,961,529.00 (10%)

括號內((10%))是市場份額。

我正在使用Visual Studio 2008,C#。

+0

你嘗試過什麼了嗎? - 顯示你有的代碼以及它沒有工作。 – 2013-03-15 13:24:20

+1

你把它當作一個字符串嗎?然後,您需要通過將值和市場份額分成兩個兩個字符串並將這些字符串解析爲數字來解析字符串,然後您需要將結果格式化爲一個新字符串,如SpaceApples回答 – 2013-03-15 13:26:23

回答

2

的輸出使用正則表達式排序你可以從字符串中獲取數字部分。然後,您可以將其解析爲整數,然後以任意方式對其進行格式化,然後再次將字符串重新組合。

public static string Format(string input) 
{ 
    Regex regex = new Regex(@"^(\d+)(.*)$"); 
    var match = regex.Match(input); 
    if (match.Success) 
    { 
     input = Int32.Parse(match.Groups[1].Value).ToString("N2"); 
     input += " " + match.Groups[2].Value; 
    } 
    return input; 
} 

你使用這樣的:

string input = "43961529(10%)"; 
string result = Format(input);  // "43,961,529.00 (10%)" 
+0

其工作,StackOverflow rocks :) – Gowtham 2013-03-15 14:07:15

+0

謝謝再次:) – Gowtham 2013-04-04 13:54:34

0

你應該看看的ToString()覆蓋

decimal num = 43961529; 
string val = num.ToSTring("N2"); 

string val = num.ToSTring("0.00"); 

有許多方法將一個字符串

相關問題