I’m writing this down because I find that I’ve googled it a couple of times and it always seems to take longer to figure out than I think it should.
Say you have a number, but you want it to be padded with leading zeroes. So, 42 => 00042. In my case, I was trying to format something similar to a serial number where I wanted a prefix with a number added on to the end, but still keeping the overall string length the same.
The easiest way
The easiest way to do this (in my opinion) is to use string interpolation and composite formatting. Its quick and easy, but requires that you remember the syntax (hence my repeated googling):
Another way
You could also convert it to a string and use the .PadLeft to prepend char ‘0’ to the front.
This works too, but it doesn’t seem as nice to me.
Obviously there are other, more inefficient ways to accomplish this (with loops and stuff), but that seems like a pointless exercise.