Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET String.Format() to add commas in thousands place for a number

I want to add a comma in the thousands place for a number.

Would String.Format() be the correct path to take? What format would I use?

like image 829
Seibar Avatar asked Sep 19 '08 21:09

Seibar


People also ask

How can I format a string number to have commas?

We can use the FORMAT() function to format numbers with commas. When we use this function, we pass the number and a format string. The format string determines how the number will be formatted when returned. The FORMAT() function returns a formatted string representation of the number, based on the values we provide.

How do I format a string in C#?

The most common way how we format strings is by using string. Format() . In C#, the string Format method is used to insert the value of the variable or an object or expression into another string.


2 Answers

String.Format("{0:n}", 1234);  // Output: 1,234.00 String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876 
like image 119
Seibar Avatar answered Oct 01 '22 12:10

Seibar


I found this to be the simplest way:

myInteger.ToString("N0") 
like image 37
alchemical Avatar answered Oct 01 '22 12:10

alchemical