Printing leading zeroes
#4
I use something like this:

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.  Wink

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
Reply


Messages In This Thread
Printing leading zeroes - by bert22306 - 03-29-2023, 04:34 AM
RE: Printing leading zeroes - by mnrvovrfc - 03-29-2023, 05:10 AM
RE: Printing leading zeroes - by mdijkens - 03-29-2023, 07:51 AM
RE: Printing leading zeroes - by bplus - 03-29-2023, 01:39 PM
RE: Printing leading zeroes - by SMcNeill - 03-29-2023, 08:00 AM
RE: Printing leading zeroes - by bert22306 - 03-29-2023, 11:39 AM
RE: Printing leading zeroes - by bert22306 - 03-29-2023, 08:16 PM
RE: Printing leading zeroes - by mdijkens - 03-30-2023, 07:22 AM
RE: Printing leading zeroes - by MrCreemy - 04-23-2023, 07:15 PM



Users browsing this thread: 5 Guest(s)