Adding commas to thousands in C#
I'm trying to add commas to the following line of code:
Console.WriteLine(String.Format("{0, 8} {1,8} {2,8}", number, square, cube));
How does one use alignment formatting in conjunction with adding commas?
It's this way {0,8:N2}
N2 will format with comma based on locale.
Output sample could be useful... This: String.Format("{0, 8}, {1,8}, {2,8}", number, square, cube));
?
Or you are looking for Number formatting that has thousands separator? Than you need to specify desired CultureInfo as first argument of the String.Format
.
Try adding in the commas to the numbers before performing the alignment formatting (modifying based on your locale/culture, if necessary):
Console.WriteLine(
String.Format("{0, 8} {1,8} {2,8}",
number.ToString("#,0"),
square.ToString("#,0"),
cube.ToString("#,0")
)
);
And as Jeff points out in his comment below, you can also accomplish this by including the comma formats inline with the alignment formatting (the first part of each format block gives the alignment, the second part formats the string):
Console.WriteLine("{0,8:#,0} {1,8:#,0} {2,8:#,0}", number, square, cube);
链接地址: http://www.djcxy.com/p/50544.html
上一篇: 整数,千位分隔符,无小数
下一篇: 在C#中向数千人添加逗号