Page History: ZPad$() - pad a number with leading zeroes
Compare Page Revisions
Page Revision: 2021/11/28 09:00
Some time it is nice to have leading zeroes on a number. MMBasic helpfully removes these for you which means to have them, the number must be returned as a string.
This tiddly Function does just that, it returns the integer value x, padded to length y with leading zeroes as a string. The same functionality can be produced with STR$() but is more involved.
RestrictionsDue to the method used for a fast result, the string passed in or the desired size, cannot exceed 127 characters. Any higher will cause an error due to the concatenation attempting to create a string longer than 255 bytes. In practice this is unlikely to be an issue.
Syntax:
=ZPad$(number,totalNumDigits)
Example:
Print ZPad$(7,3)' returns "007"
Code:
Function ZPad$(x As integer,y As integer)
ZPad$=Right$(String$(y,"0")+Str$(x),y)
End Function