Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a number in C# with commas and decimals?

Tags:

c#

.net

I have a number with a variable number of digits after the decimal point. I want to format the number with commas and all decimal numbers.

For example: 42,023,212.0092343234

If I use ToString("N") I get only 2 decimals, ToString("f") gives me all decimals no commas. How do I get both?

like image 809
NotDan Avatar asked Jul 22 '10 18:07

NotDan


People also ask

What is %s %d %F in C?

It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc. The format specifier in printf() and scanf() are mostly the same but there is some difference which we will see.

For what %D is used in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer.

What is %s in C?

Note: The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to print and read strings directly.

What does %3d in C mean?

Literally, it means to print an integer padded to three digits with spaces. The % introduces a format specifier, the 3 indicates 3 digits, and the d indicates an integer.


1 Answers

Not sure (and unable to test right now) but would something like this work?

"#,##0.################"
like image 52
pdr Avatar answered Sep 28 '22 01:09

pdr