Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What do you like to use for adding commas to numerical output?
#1
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
Reply
#2
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
Shoot first and shoot people who ask questions, later.
Reply




Users browsing this thread: 1 Guest(s)