String.Format an integer to use a thousands separator without decimal places or leading 0 for small integers

Justin picture Justin · Nov 3, 2009 · Viewed 62.2k times · Source

Silly question, I want to format an integer so that it appears with the 1000's separator (,), but also without decimal places and without a leading 0.

My attempts so far have been:

String.Format("{0} {1}", 5, 5000);            // 5 5000
String.Format("{0:n} {1:n}", 5, 5000);        // 5.00 5,000.00
String.Format("{0:0,0} {1:0,0}", 5, 5000);    // 05 5,000

The output I'm after is:

5 5,000

Is there something obvious that I'm missing?

Answer

Richard Friend picture Richard Friend · Nov 3, 2009

This worked for me.

String.Format("{0:#,0} {1:#,0}", 5, 5000); // 5 5,000