home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / homonlib.zip / LEADZERO.BAS < prev    next >
BASIC Source File  |  1995-04-13  |  2KB  |  48 lines

  1. DEFINT A-Z
  2.  
  3. DECLARE FUNCTION LeadZero$ (number, newlen)
  4.  
  5. FUNCTION LeadZero$ (number, newlen)
  6. '****************************************************************************
  7. '"Stringifys" an integer and pads it on the left with leading zeros up to the
  8. ' desired length.
  9. '
  10. 'This function was created mainly due to PRINT USING's inability to add
  11. ' leading zeros (But you can add asterisks!  Gee, I use that a lot.  NOT!!).
  12. ' Feel free to create additional functions that work on other data types.
  13. '
  14. 'Note: If used on a negative number, the minus sign will be included when
  15. ' calculating the new length.
  16. '
  17. '    Examples: LeadZero$(5,5)  -->  "00005"
  18. '              LeadZero$(-5,5) -->  "-0005"
  19. '
  20. '****************************************************************************
  21.  
  22. new$ = LTRIM$(STR$(number))        'Turn the number into a string.
  23.  
  24. l = LEN(new$)                      'Find the length of the new string.
  25.  
  26. IF newlen <= l THEN                'If the string is equal to or longer than
  27.      LeadZero$ = new$              'the new length, return the original.
  28.      EXIT FUNCTION
  29. END IF
  30.  
  31. IF number < 0 THEN                 'If the number is negative, remove the -.
  32.      new$ = RIGHT$(new$, (l - 1))
  33. END IF
  34.  
  35. diff = newlen - l                  'Find the difference in lengths.
  36.  
  37. new$ = STRING$(diff, "0") + new$   'Pad the string with leading zeros.
  38.  
  39. IF number < 0 THEN                 'If the number is negative, replace the -.
  40.      new$ = "-" + new$             'Its length will now be equal to newlen
  41. END IF                             'since we measured its length before
  42.                                    'removing the -.
  43.  
  44. LeadZero$ = new$                   'Return the finished product.
  45.  
  46. END FUNCTION
  47.  
  48.