-5
int num = 00;
string s=num.ToString();

It is giving value "0". The expected result is "00". How to do this?

Can anyone Help me?

Ernesto
  • 1,523
  • 1
  • 14
  • 32
Mahtab Alam
  • 315
  • 3
  • 8
  • 3
    `string s=num.ToString("D2");` – itsme86 May 15 '17 at 16:16
  • 4
    Are you expecting `int num = 00;` and `int num = 0;` to do different things? If you are, you're going to be disappointed. – Jon Skeet May 15 '17 at 16:19
  • Possible duplicate of [C# convert int to string with padding zeros?](http://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros) – Ernesto May 15 '17 at 16:22

2 Answers2

1

It's in your value assignment. An int of 00 always has value 0. If you want to always show two digits, you need to apply a format to your ToString() call

int num = 00;
string s= num.ToString("D2");   

That should do the trick.

CDove
  • 1,940
  • 10
  • 19
0

You can achieve the desired result using the implementation of the ToString() method passing a parameter "D2" as shown in the demo link: http://rextester.com/JKIDK14711 Or you can get more information about the parameters that ToString() accepts from here: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx