QB64 Phoenix Edition
What do you like to use for adding commas to numerical output? - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: Chatting and Socializing (https://staging.qb64phoenix.com/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://staging.qb64phoenix.com/forumdisplay.php?fid=2)
+--- Thread: What do you like to use for adding commas to numerical output? (/showthread.php?tid=458)



What do you like to use for adding commas to numerical output? - Pete - 05-21-2022

I've never used PRINT USING in my programs, so I usually code something like this demo...

Code: (Select All)
DIM a AS _INTEGER64
DO
    INPUT a
    a$ = LTRIM$(STR$(ABS(a)))
    j = LEN(a$) MOD 3: IF j = 0 THEN j = 3
    DO UNTIL j >= LEN(a$)
        a$ = MID$(a$, 1, j) + "," + MID$(a$, j + 1)
        j = j + 4
    LOOP
    IF a < 0 THEN a$ = "-" + a$
    PRINT a$ ' Output with commas.
LOOP

Pete


RE: What do you like to use for adding commas to numerical output? - Pete - 05-22-2022

Something a bit more optimized.

Code: (Select All)
DO
    INPUT a
    a$ = LTRIM$(STR$(a))
    j = 4
    DO UNTIL j / 4 >= LEN(LTRIM$(STR$(ABS(a)))) / 3
        a$ = MID$(a$, 1, LEN(a$) + 1 - j) + "," + MID$(a$, LEN(a$) + 1 - j + 1)
        j = j + 4
    LOOP
    PRINT a$
LOOP

Of course for very large numbers this would have to be tweaked to exclude numeric input, and just use string math.

Code: (Select All)
DO
    LINE INPUT a$
    x = LEN(a$) : IF LEFT$(a$, 1) = "-" THEN x = x - 1
    j = 4
    DO UNTIL j / 4 >= x / 3
        a$ = MID$(a$, 1, LEN(a$) + 1 - j) + "," + MID$(a$, LEN(a$) + 1 - j + 1)
        j = j + 4
    LOOP
    PRINT a$
LOOP

Pete