How to increment a number with leading zeros

I’ve needed to increment a number by one while keeping the leading zeros. For example 0001, 0002, 0003, 0004, and so on. I wasn’t sure of a sure way to do this in C# but I found the following works very well.

nextNumber.ToString().PadLeft(4, Convert.ToChar(“0”))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.