03-29-2023, 08:00 AM
I use something like this:
Note: This is for positive numbers only, as I didn't need negative values when I wrote it, though you should be able to easily expand for that if necessary.
What does the above do? It lets you specify a value for leading and trailing zeroes, to keep formatting consistent with your needs.
00.10 <-- first output
03 <-- second output, with no decimal place as we specified not to have one.
03.00 <-- third output with 2 decimal points
Code: (Select All)
Print PaddedString(0.1, 2, 5)
Print PaddedString(3, 0, 2)
Print PaddedString(3, 2, 5)
Function PaddedString$ (value As _Float, decimal_places As _Byte, length As _Byte)
If length = 0 Then Exit Function
Dim tempV As _Float: tempV = value
If decimal_places Then tempV = Int(tempV * 10 ^ decimal_places + .5) / 10 ^ decimal_places
v$ = String$(255, "0") + _Trim$(Str$(tempV))
p = InStr(v$, ".")
If p Then
If decimal_places Then
v$ = Left$(v$ + String$(255, "0"), p + decimal_places)
Else
v$ = Left$(v$, p - 1)
End If
Else
If decimal_places Then v$ = v$ + "." + String$(decimal_places, "0")
End If
PaddedString$ = Right$(v$, length)
End Function
Note: This is for positive numbers only, as I didn't need negative values when I wrote it, though you should be able to easily expand for that if necessary.
What does the above do? It lets you specify a value for leading and trailing zeroes, to keep formatting consistent with your needs.
00.10 <-- first output
03 <-- second output, with no decimal place as we specified not to have one.
03.00 <-- third output with 2 decimal points