Pad Numerical Text With Leading Zeros by Steve Ancell26th Dec 2022 4:15
|
---|
Summary A handy function for converting an integer to a string that's padded with leading zeros. Description After searching for a function in AGK that preserves zero spaces, and not getting what I wanted, I decided to make my own. Handy for when you want to show leading zeros for scoreboards or other integers in games, etc... Update: Phaelax pointed out that the Right() function can be used as a simpler method, it seems like the answer was staring me right in the face but I just didn't see the obvious. Both mine and Phaelax idea work fine, it's up to you which one suits you best. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com text = CreateText(PadStringZeros(123)) SetTextSize(text, 32) do sync() loop end function PadStringZeros(num as integer) // My intitial idea local outString as string local numToString as string local padSpaces as string = "0000000" numToString = str(num) outString = mid(padSpaces, 1, len(padSpaces) - len(numToString)) + numToString if len(numToString) > len(padSpaces) outString = mid(numToString, len(numToString) - len(padSpaces) + 1, len(padSpaces)) endif endfunction outString function PadStringZeros(num as integer) // Phaelax's idea local outString as string outString = right("0000000" + str(num), 7) endfunction outString |