Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ToString("N2") and ToString("0.00")

Tags:

c#

formatting

What is the difference between ToString("N2") and ToString("0.00")?

like image 632
Rupa Mistry Avatar asked Dec 22 '10 05:12

Rupa Mistry


People also ask

What is the difference between n2 and tostring in JavaScript?

The comma can break down-stream code that might have to parse that value back to a decimal, especially client-side js. Actually "N2" will print "3,543.00". --Answer is rp="567.34"; -- N2 gives upto two decimal records. The question asked the difference between ToString ("N2") and ToString ("0.00"), which isn't covered in this answer.

How does tostring(N2) work?

Basically, ToString ("N2") will use the CultureInfo to format the number. This means that your thousands separator might be different depending on the used CultureInfo. You can also pass the desired CultureInfo if you want. Both give you two decimal places, but you can see the difference easily if you check larger numbers:

What is the difference between N and N2?

It would seem that N will include thousands separators, whereas 0.00 will not. N2 will work the same way for 500.00, but when you have 5000.00, N2 will display as

What does G MEAN in tostring?

A "G" format specifier that represents a customary or common format of the object. The parameterless overload of your object's ToString method should call its ToString(String) overload and pass it the "G" standard format string. Support for a format specifier that is equal to a null reference (Nothing in Visual Basic).


2 Answers

From Standard Numeric Format Strings

The number is converted to a string of the form "-d,ddd,ddd.ddd…", where '-' indicates a negative number symbol if required, 'd' indicates a digit (0-9), ',' indicates a thousand separator between number groups, and '.' indicates a decimal point symbol.

It would seem that N will include thousands separators, whereas 0.00 will not.

See also Custom Numeric Format Strings

like image 122
Adriaan Stander Avatar answered Sep 26 '22 12:09

Adriaan Stander


It's all about the decimal places

N2 will work the same way for 500.00, but when you have 5000.00, N2 will display as

5,000.00

instead of

5000.00

See Standard Numeric Format Strings for more information.

like image 21
Sam Jones Avatar answered Sep 25 '22 12:09

Sam Jones