[C#] How to make numbers 0-9 two digit?

Discussion in 'Mixed Languages' started by example12345678912345678, May 24, 2021.

Tags:
  1. example12345678912345678

    example12345678912345678 MDL Senior Member

    Dec 29, 2019
    371
    126
    10
    Hi guys, I'm having a problem. I want to make numbers show as "00, 01, 02 ..." But it shows like "0, 1, 2 ..." I tried everything I found on internet. This is my last try.
    Code:
     Console.WriteLine("X = {0:D2}", 0);
                Console.WriteLine("X = {0:D2}", 1);
                Console.WriteLine("X = {0:D2}", 2);
                Console.WriteLine("X = {0:D2}", 3);
                Console.WriteLine("X = {0:D2}", 4);
                Console.WriteLine("X = {0:D2}", 5);
                Console.WriteLine("X = {0:D2}", 6);
                Console.WriteLine("X = {0:D2}", 7);
                Console.WriteLine("X = {0:D2}", 8);
                Console.WriteLine("X = {0:D2}", 9);
    But nothing worked. I need some help from you. Thank you so much.
     
  2. nosirrahx

    nosirrahx MDL Expert

    Nov 7, 2017
    1,172
    566
    60
    As usual, MS docs add 500 mountains of crap like the good little gatekeepers they are.

    int Number = 500;
    Console.WriteLine(Number.ToString("D8"));

    This will output 8 total characters where the characters before the 500 are all 0s.