Printing leading zeroes
#2
This is what I've designed and used for a long time:

Code: (Select All)
'this allows a negative value padded by zeroes after sign
'set "numdig" to negative value to convert "num" to hexadecimal
'eg. "numdig = -6" to convert "num" to 6-digit hexadecimal string
FUNCTION Zeroes$ (num AS LONG, numdig AS INTEGER)
DIM b$, hx AS LONG, sg AS LONG, v AS LONG
IF num < 0 THEN sg = -1: num = num * -1
IF numdig < 0 THEN hx = 1: numdig = numdig * -1 ELSE hx = 0
IF hx THEN
    b$ = HEX$(num)
ELSE
    b$ = LTRIM$(STR$(num))
END IF
v = numdig - LEN(b$)
IF v > 0 THEN b$ = STRING$(v, 48) + b$
IF sg = -1 THEN b$ = "-" + b$
Zeroes$ = b$
END FUNCTION

Call:

Code: (Select All)
DIM a$, nl AS LONG
nl = 99999
a$ = Zeroes$(nl, -6)

"a$" then should give you a hexadecimal number of six digits padded by zeroes. "01869F", if you need the "&H" before it you will have to concatenate it separately.

Otherwise the second parameter should be greater than zero, for the number of digits you want in front of a decimal number.
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: 3 Guest(s)