need track number to filename help

When i format the filename i want to include the track number with leading zeros determined by how many total tracks there are (using a/b style track tag with no leading zeros to support both mp3 and m4a tags.)

1/9 would be 01. (i always want at least one leading 0 for 1-9)
1/99 would be 01.
1/100 would be 001.
1/1000 would be 0001. (Ive never seen an album with more than 999 songs, but support for it would be nice)

the following code works (up to 999), but is terribly long and i feel like there MUST be a more efficient way of doing this... any one got a better solution?

I don't know if better, but different:
This checks the length of the string behind the /.
So 1/9 stays the same - or you update the total to 09.