投票最多的答案很棒,并且已经帮助了大约7年。随着C#6.0的引入,特别是字符串插值,有一种更整洁、更安全的方法来完成被问到的to add commas in thousands place for a number:
var i = 5222000;var s = $"{i:n} is the number"; // results to > 5,222,000.00 is the numbers = $"{i:n0} has no decimal"; // results to > 5,222,000 has no decimal
var jackpot = 1_000_000; // underscore separators in numeric literals also available since C# 7.0var niceNumberString = $"Jackpot is {jackpot:n}";var niceMoneyString = $"Jackpot is {jackpot:C}";