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?
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);
ToString() here, just tack on the format to in the parameters. Console.WriteLine("{0,8:0,0} {1,8:0,0} {2,8:0,0}", number, square, cube);