Author Topic: Comma delimited numbers with decimal len  (Read 4801 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Comma delimited numbers with decimal len
« on: December 03, 2018, 06:17:12 pm »
Love QB64 (I put it 1 step higher than pascal, only because QB64 can do full screen pascal cannot)
OK I digress,
I am sure this code I made is an over kill, however, It works, but sometimes it is putting a "," in front of the  number

example instead of
295,559.00
it does
,295,559.00

If there is a better method that would be great

I do not want to use a print using "###.##" etc... I need a return string

thanks for looking


Code: QB64: [Select]
  1. FUNCTION NUM2STR$ (x AS DOUBLE, PRECISION AS INTEGER)
  2.     DIM I, J AS INTEGER
  3.     '// x           = number to convert
  4.     '// precision   = how many decimals places to the right
  5.    
  6.  
  7.     DIM STR_X AS STRING
  8.     DIM DECPOS AS INTEGER
  9.     DIM DEC_STR AS STRING
  10.     DIM INT_STR AS STRING
  11.     DIM COMMA AS STRING
  12.  
  13.     '// If less than 1,000 then no comma needed
  14.     '// exit sub
  15.     STR_X = STR$(x)
  16.     IF x < 1000 THEN
  17.         NUM2STR = STR_X
  18.         EXIT SUB
  19.     END IF
  20.  
  21.     '// FIND A DECIMAL
  22.     DECPOS = INSTR(STR_X, ".")
  23.  
  24.     '// WORK ON DECIMAL SECTION
  25.     IF DECPOS = 0 THEN '                        // NO DECIMAL?
  26.         DEC_STR = ".00"
  27.     ELSE
  28.         DEC_STR = MID$(STR_X, DECPOS, PRECISION + 1)
  29.     END IF
  30.  
  31.     '// If there is a decimal, calc string, otherwise the int_str = the string
  32.     IF DECPOS = 0 THEN
  33.         INT_STR = STR_X
  34.     ELSE
  35.         INT_STR = LEFT$(STR_X, DECPOS - 1)
  36.     END IF
  37.  
  38.  
  39.     COMMA = ""
  40.     J = 0
  41.     FOR I = LEN(INT_STR) TO 0 STEP -1
  42.         J = J + 1
  43.         IF J = 4 THEN
  44.             COMMA = MID$(INT_STR, I, 1) + "," + COMMA
  45.             J = 0
  46.         ELSE
  47.             COMMA = MID$(INT_STR, I, 1) + COMMA
  48.         END IF
  49.     NEXT
  50.     NUM2STR$ = COMMA + DEC_STR
  51.  
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Comma delimited numbers with decimal len
« Reply #1 on: December 03, 2018, 07:22:28 pm »
Isn't it a case of you manually adding the comma with:

NUM2STR$ = COMMA + DEC_STR
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Comma delimited numbers with decimal len
« Reply #2 on: December 03, 2018, 07:41:52 pm »
That's not it. The variable isn't necessarily a comma. Try some numbers. The algorithms are just off. 100 doesn't add .00, but 1000 does. 1000 outputs 1,000.00 but 234567 outputs , 234,567 and 2345678 outputs , 2345,678.

Added input code so others could try numbers...

Code: QB64: [Select]
  1. INPUT "Number: "; x
  2. y$ = NUM2STR$(x, 0)
  3.  
  4. FUNCTION NUM2STR$ (x AS DOUBLE, PRECISION AS INTEGER)
  5. '// x           = number to convert
  6. '// precision   = how many decimals places to the right
  7.  
  8.  
  9. DIM STR_X AS STRING
  10. DIM DECPOS AS INTEGER
  11. DIM DEC_STR AS STRING
  12. DIM INT_STR AS STRING
  13. DIM COMMA AS STRING
  14.  
  15. '// If less than 1,000 then no comma needed
  16. '// exit sub
  17. STR_X = STR$(x)
  18. IF x < 1000 THEN
  19.     NUM2STR = STR_X
  20.     EXIT SUB
  21.  
  22. '// FIND A DECIMAL
  23. DECPOS = INSTR(STR_X, ".")
  24.  
  25. '// WORK ON DECIMAL SECTION
  26. IF DECPOS = 0 THEN '                        // NO DECIMAL?
  27.     DEC_STR = ".00"
  28.     DEC_STR = MID$(STR_X, DECPOS, PRECISION + 1)
  29.  
  30. '// If there is a decimal, calc string, otherwise the int_str = the string
  31. IF DECPOS = 0 THEN
  32.     INT_STR = STR_X
  33.     INT_STR = LEFT$(STR_X, DECPOS - 1)
  34.  
  35.  
  36. COMMA = ""
  37. J = 0
  38. FOR I = LEN(INT_STR) TO 0 STEP -1
  39.     J = J + 1
  40.     IF J = 4 THEN
  41.         COMMA = MID$(INT_STR, I, 1) + "," + COMMA
  42.         J = 0
  43.     ELSE
  44.         COMMA = MID$(INT_STR, I, 1) + COMMA
  45.     END IF
  46. NUM2STR$ = COMMA + DEC_STR
  47.  
  48.  

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Marked as best answer by xra7en on December 05, 2018, 08:11:58 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Comma delimited numbers with decimal len
« Reply #3 on: December 03, 2018, 08:44:45 pm »
6 of 1, h,alf do,zen of a,not,her:
Code: QB64: [Select]
  1. _TITLE "Commatose Test" 'B+  2018-12-03
  2. COLOR 2, 0
  3. FOR i = 1 TO 20
  4.     test = .123456789123456789123456789 * 10 ^ i
  5.     PRINT Commatose$(test, 2)
  6.     INPUT "Enter a number to test Commatose ", test
  7.     PRINT Commatose$(test, 4)
  8. LOOP UNTIL test = 0
  9.  
  10.  
  11. FUNCTION Commatose$ (v AS DOUBLE, precision AS INTEGER)
  12.     sv$ = LTRIM$(STR$(v)) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ltrim$ remove leading space!
  13.     dot = INSTR(sv$, ".")
  14.     IF dot THEN
  15.         i$ = MID$(sv$, 1, dot - 1)
  16.         d$ = LEFT$(MID$(sv$, dot + 1) + STRING$(precision, "0"), precision)
  17.     ELSE
  18.         i$ = sv$
  19.         d$ = STRING$(precision, "0")
  20.     END IF
  21.     FOR i = LEN(i$) TO 1 STEP -1
  22.         c = c + 1
  23.         IF c = 4 THEN c = 1: b$ = "," + b$
  24.         b$ = MID$(i$, i, 1) + b$
  25.     NEXT
  26.     Commatose$ = b$ + "." + d$
  27.  
  28.  

Yes sir, that IS half of a Christmas tree! ;-))
« Last Edit: December 03, 2018, 08:55:55 pm by bplus »

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Comma delimited numbers with decimal len
« Reply #4 on: December 05, 2018, 10:13:02 am »
6 of 1, h,alf do,zen of a,not,her:
Code: QB64: [Select]
  1.  
  2.     sv$ = LTRIM$(STR$(v)) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ltrim$ remove leading space!
  3.    
  4.  
  5.  

Yes sir, that IS half of a Christmas tree! ;-))

Yup the guilty line! Thanks!!!
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Comma delimited numbers with decimal len
« Reply #5 on: December 05, 2018, 11:00:20 am »
Thanks, I forget that leading space more times than I care to remember. :)

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Comma delimited numbers with decimal len
« Reply #6 on: December 06, 2018, 12:21:08 pm »
Thanks, I forget that leading space more times than I care to remember. :)

its funny the qb64 did not add a trim function, they have left/righ but nothing for all
strings they do, left/right and mid meh simple function to solve that :-)

Code: QB64: [Select]
  1. function trim(txt as string)
  2.     trim = ltrim$(rtrim$(txt))

I think that does it :P
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: Comma delimited numbers with decimal len
« Reply #7 on: December 06, 2018, 12:42:18 pm »
The development build has _TRIM$(), which is a macro for LTRIM$(RTRIM$()), and it'll be available in the next version yet to be released.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Comma delimited numbers with decimal len
« Reply #8 on: December 06, 2018, 12:56:13 pm »
The development build has _TRIM$(), which is a macro for LTRIM$(RTRIM$()), and it'll be available in the next version yet to be released.

suweet!! I downloaded the latest today from git, not much changed in this version - still seems to be at 1.2 (the last one I had was 2/2/2018)
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: Comma delimited numbers with decimal len
« Reply #9 on: December 06, 2018, 01:09:24 pm »
You probably grabbed from the master branch instead of the development branch. Get it from qb64.org instead.