Search code examples
c#string-formatting

Format any number to three digits?


I want to insert any number was entered by the user, to the database like a three digits.

e.g. if the user insert ( 1 ) i want to save the number as ( 001 ), or ( 20 ) to ( 020 ).

note: The data type of the database column is string not integer.


Solution

  • int iVal = 1;
    
    iVal.ToString("D3"); // = "001"
    

    Read more on MSDN