I added leading zeros by defining "pad" as a space (default), and then changing pad to "0" should the width parameter be preceded by a zero.
My code is probably terrible I lifted this from this very thread and modified it, but here it is:
Code: Select all
Macro __Format__(text, max, pad)
If max
If max < 0
max = ~max
max + 1
If Len(text) <= max
text = LSet(text, max, pad)
EndIf
Else
If Len(text) <= max
text = RSet(text, max, pad)
EndIf
EndIf
EndIf
EndMacro
Procedure.s Format(text.s, *value1 = 0, *value2 = 0, *value3 = 0, *value4 = 0, *value5 = 0, *value6 = 0, *value7 = 0, *value8 = 0, *value9 = 0, *value10 = 0, *value11 = 0)
Protected zeiger, *value, pos1, pos2, len1, len2, exit
Protected type, result.s, help.s, Format.s
Protected is_op = #False
Protected pad.s = " "
zeiger = @*value1
result = text
result = ReplaceString(result, "\r", #CR$)
result = ReplaceString(result, "\n", #LF$)
result = ReplaceString(result, "\t", #TAB$)
result = ReplaceString(result, "\\", "\")
Repeat
pos1 = FindString ( result, "%", pos1 )
If pos1
*value = PeekL ( zeiger )
pos2 = pos1 + 1
exit = #False
Repeat
type = Asc ( Mid ( result, pos2, 1 ) )
is_op = #False
Select type
Case 98, 119, 108, 113, 102, 100, 115 ; "b", "w", "l", "q", "f", "d", "s"
Format = Mid ( result, pos1 + 1, pos2 - pos1 - 1 )
len1 = Val ( StringField ( Format, 1, "." ) )
len2 = Val ( StringField ( Format, 2, "." ) )
is_op = #True
EndSelect
Select type
Case 98
help = Str ( PeekB ( *value ) ) ; "b"
Case 119
help = Str ( PeekW ( *value ) ) ; "w"
Case 108
help = Str ( PeekL ( *value ) ) ; "l"
Case 113
help = Str ( PeekQ ( *value ) ) ; "q" (StrQ depricated so changed to Str)
Case 102
help = StrF ( PeekF ( *value ), len2 ) ; "f"
Case 100
help = StrD ( PeekD ( *value ), len2 ) ; "d"
Case 115
help = PeekS ( *value ) ; "s"
Case 37
help = "%"
result = Left ( result, pos1 - 1 ) + Mid ( result, pos2, Len(result)) ; "%"
Case 43, 45, 46 ; "-" "+" "."
pos2 + 1
Case 49 To 57 ; "1" "2" "3" "4" "5" "6" "7" "8" "9"
pos2 + 1
Case 48 ; "0"
pos2 + 1
pad = "0"
Default
exit = #True
EndSelect
If is_op = #True
__Format__( help, len1, pad )
result = Left ( result, pos1 - 1 ) + help + Mid ( result, pos2 + 1, Len(result))
zeiger + 4
exit = #True
EndIf
Until exit
pos1 + Len ( help )
Else
Break
EndIf
ForEver
ProcedureReturn result
EndProcedure