53

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

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Rupa Mistry
  • 639
  • 2
  • 7
  • 7

6 Answers6

42

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

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
25

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.

Sam Jones
  • 4,443
  • 2
  • 40
  • 45
17

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.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
arbbot
  • 303
  • 2
  • 6
10

Both give you two decimal places, but you can see the difference easily if you check larger numbers:

var d = 1234567.89;
for (var i = 0; i < 10; ++i) {
    Console.WriteLine(d.ToString("N2") + "\t" + d.ToString("0.00"));
    d /= 10.0;
}

outputs

1.234.567,89    1234567,89
123.456,79  123456,79
12.345,68   12345,68
1.234,57    1234,57
123,46  123,46
12,35   12,35
1,23    1,23
0,12    0,12
0,01    0,01
0,00    0,00

Execute code online at dotnetfiddle.net

Karsten Gutjahr
  • 316
  • 2
  • 12
3

The thousand separator is an issue. Using "n2" will give you 3,543 where using "0.00" will give you 3543. The comma can break down-stream code that might have to parse that value back to a decimal, especially client-side js.

markd
  • 81
  • 5
-8

Here is example to explain

int rupees=567.3423;
string rp=rupees.tostring("N2");

--Answer is rp="567.34"; -- N2 gives upto two decimal records.

Robert
  • 5,278
  • 43
  • 65
  • 115
  • 2
    The question asked the difference between `ToString("N2")` and `ToString("0.00")`, which isn't covered in this answer. Besides that, the answer is just straight up wrong since `N2`, as explained by the other answers, will keep `CultureInfo` in mind and will add a thousands separators. – Wouter Vanherck Jul 25 '18 at 07:07
  • 3
    either way, an int can't have a decimal place – Sam Jones Dec 16 '19 at 16:11