home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 24
/
CD_ASCQ_24_0995.iso
/
vrac
/
homonlib.zip
/
LEADZERO.BAS
< prev
next >
Wrap
BASIC Source File
|
1995-04-13
|
2KB
|
48 lines
DEFINT A-Z
DECLARE FUNCTION LeadZero$ (number, newlen)
FUNCTION LeadZero$ (number, newlen)
'****************************************************************************
'"Stringifys" an integer and pads it on the left with leading zeros up to the
' desired length.
'
'This function was created mainly due to PRINT USING's inability to add
' leading zeros (But you can add asterisks! Gee, I use that a lot. NOT!!).
' Feel free to create additional functions that work on other data types.
'
'Note: If used on a negative number, the minus sign will be included when
' calculating the new length.
'
' Examples: LeadZero$(5,5) --> "00005"
' LeadZero$(-5,5) --> "-0005"
'
'****************************************************************************
new$ = LTRIM$(STR$(number)) 'Turn the number into a string.
l = LEN(new$) 'Find the length of the new string.
IF newlen <= l THEN 'If the string is equal to or longer than
LeadZero$ = new$ 'the new length, return the original.
EXIT FUNCTION
END IF
IF number < 0 THEN 'If the number is negative, remove the -.
new$ = RIGHT$(new$, (l - 1))
END IF
diff = newlen - l 'Find the difference in lengths.
new$ = STRING$(diff, "0") + new$ 'Pad the string with leading zeros.
IF number < 0 THEN 'If the number is negative, replace the -.
new$ = "-" + new$ 'Its length will now be equal to newlen
END IF 'since we measured its length before
'removing the -.
LeadZero$ = new$ 'Return the finished product.
END FUNCTION