C#: .net String.Format() To Add Commas In Thousands Place For A Number

To add commas in the thousands place for a number using C# and the .NET String.Format() method, you can use the N format specifier with a specific number of decimal places. Here’s an example:

int number = 12345678;
string formattedNumber = String.Format("{0:N0}", number);
Console.WriteLine(formattedNumber);

Output:

12,345,678

In the above code, {0:N0} is the format specifier. 0 is the placeholder for the number to be formatted, and N0 specifies that the number should be formatted with zero decimal places and with commas in the thousands place.

You can modify the code based on your specific requirement or use with different numeric types like double, decimal, etc. Just replace the number variable with your desired numeric value.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!