Search code examples
c#.netstring-formattingtostring

How do I format a string with both trailing and leading zeros?


What is the ToString format needed to produce this?

>     5  to 00000500
>     10 to 00001000
>     15 to 00001500
>     20 to 00002000

Solution

  • If you want do this purely with a ToString format string, rather than involving a multiplication too, then you could use something like this:

    string yourString = yourNumber.ToString("000000'00'");