How do I print “1” as “0001” ?

Yep, a nice little question. Some of you may have wondering how to do that. In my database ID field it says “1” (or any integer value) on my Web Page or Windows Form I need to print this as “0001”. Do we have a simplest method to do that? And the answer is (drum roll) ….. YES ..

OK, next question what does this format specifier “D” means. Here is the MSDN help about it.

The “D” (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. This format is supported only for integral types.

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros.

Also we can use another string method known as . Lets have a look on that code.

allows you to apply any character (type of Char) as the leading character. In this case ‘0’

Have Fun 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *