QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: xra7en on December 17, 2018, 04:15:53 pm

Title: Help formatting numbers without printing to screen
Post by: xra7en on December 17, 2018, 04:15:53 pm
is there a way to do the following (pseudo code)

var = print using "###,.##"

print var
Title: Re: Help formatting numbers without printing to screen
Post by: odin on December 17, 2018, 04:21:09 pm
This topic has been renamed to properly summarize the query it contains.
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 17, 2018, 05:00:06 pm
thanks wasn't quite sure how to title that.

there is a real reason for this question. I noticed with using, that it has  massive leading spaces on large numbers, something that ltrim can fix.
I tried writing a few routines to parse the DOUBLE number, but it seems like i am reinventing the wheel (print using),

example i have 23456123455.345
and the current value is 234.5
there is a massive gap to accommodate the long number it has to fill in for - unless I am doing something incorrect or missing a tag


Title: Re: Help formatting numbers without printing to screen
Post by: SMcNeill on December 17, 2018, 05:43:18 pm
Here’s a quick idea:

Make a FUNCTION that will:

Create a new temp screen.   
Direct output to that screen
Use PRINT USING to print the result as you want.
Read it back properly formatted from that screen.
Free the temp screen.

Return the formatted string back and END FUNCTION.

Title: Re: Help formatting numbers without printing to screen
Post by: FellippeHeitor on December 17, 2018, 05:47:06 pm
I wrote one exactly like that description but it is currently being held hostage in the lost realm of our old forum. Bummer.
Title: Re: Help formatting numbers without printing to screen
Post by: TempodiBasic on December 17, 2018, 06:02:29 pm
Hi
I've just a little fallen in confusion...
in pseudocode
Quote
variable = calling PRINT USING template$
..........
.........
print variable

sorry but cannot you solve in this manner
in pseudocode
Quote
template$ =  "###,.##"
...............
...............
print using template$

what do you mean with

Quote
variable = calling PRINT USING template$
???
http://qb64.org/wiki/PRINT_USING (http://qb64.org/wiki/PRINT_USING)

taken from wiki
Code: QB64: [Select]
  1. money = 12345.45
  2. tmp1$ = "#######,.##"
  3. tmp2$ = "#####,.##"
  4. tmp3$ = "##,.##"
  5.  
  6. PRINT "I have this much money!"; USING tmp1$; money
  7. PRINT "I have this much money!"; USING tmp2$; money
  8. PRINT "I have this much money!"; USING tmp3$; money
  9.  
it seems that the template manages the more spaces before the number,
so if you must print different range of numbers you must adapt the template of PRINT USING to the lenght of the numeral variables...
just a function like this
Code: QB64: [Select]
  1.  money = 12345.45
  2. FOR i% = 10 TO 1 STEP -1
  3.     print_Using_Set i%, money
  4.  
  5.  
  6. SUB print_Using_Set (NDigit%, number AS SINGLE)
  7.     DIM template AS STRING
  8.     template = STRING$(NDigit% - 2, "#") + ",.##"
  9.     PRINT " You have this money to spend!"; USING template; number
  10.  

Hi guys, I'm slow to write, but I post the same my answer.
:-P
Title: Re: Help formatting numbers without printing to screen
Post by: RhoSigma on December 17, 2018, 07:05:27 pm
You could use one of  this tree functions, you may even extend it for more arguments:
BTW - This is the function Steve and Fellippe mentioned.
Code: QB64: [Select]
  1. '---------------------------------------------------------------------
  2. 'Function:  Can return a PRINT USING formatted string for assignment to
  3. '           any string variable. This is a multiple name function for
  4. '           use of upto three arguments (Format$, Format2$, Format3$).
  5. '
  6. 'Synopsis:  res$ = Format$  (fmt$, arg$, typ%)
  7. '           res$ = Format2$ (fmt$, arg$, typ%, arg2$, typ2%)
  8. '           res$ = Format3$ (fmt$, arg$, typ%, arg2$, typ2% arg3$, typ3%)
  9. '
  10. 'Result:    res$ --> the resulting formatted string
  11. '
  12. 'Inputs:    fmt$ --> the string with format options for PRINT USING
  13. '           arg$ --> the argument(s) to format into the fmt$ string,
  14. '                    use STR$(num) to pass in a number
  15. '           typ% --> the type(s) of the respective argument(s),
  16. '                     0 = argument is a real (alphanumeric) string
  17. '                     1 = argument is a STR$() number string
  18. '
  19. 'Notes:     This function is adapted and slightly altered from an idea
  20. '           posted in the QB64 Forum by Fellippe Heitor,
  21. '           see http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=14219.0
  22. '---------------------------------------------------------------------
  23. FUNCTION Format$ (fmt$, arg$, typ%)
  24. shan& = _SOURCE: dhan& = _DEST: than& = _NEWIMAGE(256, 1, 0) 'if results may get longer, then raise the 256 value
  25. _SOURCE than&: _DEST than&
  26. IF typ% THEN
  27.     PRINT USING fmt$; VAL(arg$);
  28.     PRINT USING fmt$; arg$;
  29. FOR i% = 1 TO POS(0) - 1
  30.     res$ = res$ + CHR$(SCREEN(1, i%))
  31. NEXT i%
  32. _SOURCE shan&: _DEST dhan&: _FREEIMAGE than&
  33. Format$ = res$
  34. '-----
  35. FUNCTION Format2$ (fmt$, arg$, typ%, arg2$, typ2%)
  36. shan& = _SOURCE: dhan& = _DEST: than& = _NEWIMAGE(256, 1, 0) 'if results may get longer, then raise the 256 value
  37. _SOURCE than&: _DEST than&
  38. IF typ% AND typ2% THEN
  39.     PRINT USING fmt$; VAL(arg$); VAL(arg2$);
  40.     PRINT USING fmt$; VAL(arg$); arg2$;
  41. ELSEIF typ2% THEN
  42.     PRINT USING fmt$; arg$; VAL(arg2$);
  43.     PRINT USING fmt$; arg$; arg2$;
  44. FOR i% = 1 TO POS(0) - 1
  45.     res$ = res$ + CHR$(SCREEN(1, i%))
  46. NEXT i%
  47. _SOURCE shan&: _DEST dhan&: _FREEIMAGE than&
  48. Format2$ = res$
  49. '-----
  50. FUNCTION Format3$ (fmt$, arg$, typ%, arg2$, typ2%, arg3$, typ3%)
  51. shan& = _SOURCE: dhan& = _DEST: than& = _NEWIMAGE(256, 1, 0) 'if results may get longer, then raise the 256 value
  52. _SOURCE than&: _DEST than&
  53. IF typ% AND typ2% AND typ3% THEN
  54.     PRINT USING fmt$; VAL(arg$); VAL(arg2$); VAL(arg3$);
  55. ELSEIF typ% AND typ2% THEN
  56.     PRINT USING fmt$; VAL(arg$); VAL(arg2$); arg3$;
  57. ELSEIF typ% AND typ3% THEN
  58.     PRINT USING fmt$; VAL(arg$); arg2$; VAL(arg3$);
  59. ELSEIF typ2% AND typ3% THEN
  60.     PRINT USING fmt$; arg$; VAL(arg2$); VAL(arg3$);
  61.     PRINT USING fmt$; VAL(arg$); arg2$; arg3$;
  62. ELSEIF typ2% THEN
  63.     PRINT USING fmt$; arg$; VAL(arg2$); arg3$;
  64. ELSEIF typ3% THEN
  65.     PRINT USING fmt$; arg$; arg2$; VAL(arg3$);
  66.     PRINT USING fmt$; arg$; arg2$; arg3$;
  67. FOR i% = 1 TO POS(0) - 1
  68.     res$ = res$ + CHR$(SCREEN(1, i%))
  69. NEXT i%
  70. _SOURCE shan&: _DEST dhan&: _FREEIMAGE than&
  71. Format3$ = res$
  72.  
  73.  
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 17, 2018, 08:03:30 pm
I wrote one exactly like that description but it is currently being held hostage in the lost realm of our old forum. Bummer.

this is the close as I got, before I put it aside:
Code: QB64: [Select]
  1. FUNCTION xnum2str$ (num AS DOUBLE, dec AS INTEGER)
  2.  
  3.     '// convert to number delimted string
  4.  
  5.     DIM number AS STRING
  6.     DIM precision AS STRING
  7.     DIM convert AS STRING
  8.     DIM tempStr AS STRING
  9.     DIM I AS INTEGER
  10.  
  11.     convert = STR$(num) '                                               convert it all to a strig
  12.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  13.     precision = MID$(convert, INSTR(convert, "."), LEN(convert)) '      store the decimal
  14.  
  15.     tempStr = ""
  16.     FOR I = LEN(number) TO 1 STEP -1 '                                  create comma delimted string
  17.         tempStr = MID$(convert, I, 1) + tempStr
  18.         IF I / 3 = INT(I / 3) THEN tempStr = "," + tempStr
  19.     NEXT
  20.     number = tempStr
  21.  
  22.  
  23.     tempStr = "" '                                                      adjust len of precisio to match
  24.     FOR I = 1 TO LEN(precision)
  25.         tempStr = tempStr + MID$(precision, I, 1)
  26.         IF I >= dec THEN EXIT FOR
  27.     NEXT
  28.     precision = tempStr
  29.     xnum2str$ = number + precision
  30.  

The problem is there is a flicker of one of the ","

Other than that it works fine. But seems like an over kill
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 17, 2018, 08:12:34 pm
Hi
I've just a little fallen in confusion...
in pseudocode

Here is what I was trying to achieve. Print using ##### etc... works fine, but, it has some padding that I do not want.
Code: [Select]
FUNCTION num2str(num as DOUBLE)
    num2str = print using "####,.##" 'obviously this DOES NOT WORK just for example

END FUNCTION

THE function print using does all the work. but does not allow you to display it as a variable.
hope that makes sense
Title: Re: Help formatting numbers without printing to screen
Post by: SMcNeill on December 17, 2018, 08:31:11 pm
Code: QB64: [Select]
  1. PRINT format$("###.###", "123.456789")
  2. PRINT format$("###,.##", "123456789.987654321")
  3.  
  4.  
  5. FUNCTION format$ (template AS STRING, text AS STRING)
  6.     d = _DEST: s = _SOURCE
  7.     n = _NEWIMAGE(80, 80, 0)
  8.     _DEST n: _SOURCE n
  9.     PRINT USING template; VAL(text)
  10.     FOR i = 1 TO 79
  11.         t$ = t$ + CHR$(SCREEN(1, i))
  12.     NEXT
  13.     format$ = t$
  14.     _DEST d: _SOURCE s
  15.     _FREEIMAGE n
  16.  

How about something like the above?  Short, simple, and easy enough to work with in most cases.
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 17, 2018, 09:16:56 pm
@SMcNeil
I tried that code, but as you can see, i fyou do not have enough hashes you get your (unwanted) symbols at the beginning of the string. so I was attempting to make a function that would return the numeric value as a string as "print using" would do if it did not have those limitations

I just thought of something else, is qb64 open source? if so, where is the routine that creates the "print using" - since i can read many language, maybe I can use that has a foundation and create a simple function
(sounds boring eh? lol)
Title: Re: Help formatting numbers without printing to screen
Post by: SMcNeill on December 17, 2018, 09:44:33 pm
Can’t you just strip out any unwanted symbols?

Code: QB64: [Select]
  1. PRINT format$("###.###", "123.456789")
  2. PRINT format$("###,.##", "123456789.987654321")
  3.  
  4.  
  5. FUNCTION format$ (template AS STRING, text AS STRING)
  6.     d = _DEST: s = _SOURCE
  7.     n = _NEWIMAGE(80, 80, 0)
  8.     _DEST n: _SOURCE n
  9.     PRINT USING template; VAL(text)
  10.     FOR i = 1 TO 79
  11.         t$ = t$ + CHR$(SCREEN(1, i))
  12.     NEXT
  13.     IF LEFT$(t$,1) = “%” THEN t$ = MID$(t$,2)
  14.     format$ = t$
  15.     _DEST d: _SOURCE s
  16.     _FREEIMAGE n
Title: Re: Help formatting numbers without printing to screen
Post by: bplus on December 18, 2018, 12:42:50 am
I am curious what is the difference with what is wanted here and this:
https://www.qb64.org/forum/index.php?topic=840.msg100462#msg100462

Do you want to fit number in a fixed string length?
Code: QB64: [Select]
  1. _TITLE "Commatose Test" 'B+  2018-12-03
  2. ' 2018-12-18 modified for fixed length
  3.  
  4. COLOR 2, 0
  5. FOR i = 1 TO 20
  6.     test = .123456789123456789123456789 * 10 ^ i
  7.     PRINT Commatose$(test, 2, 25)
  8.     INPUT "Enter a number to test Commatose ", test
  9.     PRINT Commatose$(test, 4, 25)
  10. LOOP UNTIL test = 0
  11.  
  12. FUNCTION Commatose$ (v AS DOUBLE, precision AS INTEGER, fixLength)
  13.     sv$ = LTRIM$(STR$(v)) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ltrim$ remove leading space!
  14.     dot = INSTR(sv$, ".")
  15.     IF dot THEN
  16.         i$ = MID$(sv$, 1, dot - 1)
  17.         d$ = LEFT$(MID$(sv$, dot + 1) + STRING$(precision, "0"), precision)
  18.     ELSE
  19.         i$ = sv$
  20.         d$ = STRING$(precision, "0")
  21.     END IF
  22.     FOR i = LEN(i$) TO 1 STEP -1
  23.         c = c + 1
  24.         IF c = 4 THEN c = 1: b$ = "," + b$
  25.         b$ = MID$(i$, i, 1) + b$
  26.     NEXT
  27.     Commatose$ = RIGHT$(SPACE$(fixLength) + b$ + "." + d$, fixLength)
  28.  
  29.  
Title: Re: Help formatting numbers without printing to screen
Post by: RhoSigma on December 18, 2018, 01:48:05 am
If you've no fears using some include and C-Header files, then there's still the alternative of using the QB-StdLibs, which are part of my Libraries Collection (see signature). These will give you the ability to use vprintf (or in your case vsprintf) like output formatting as in C/C++, even date/time formatting is possible. If you decide to try it, then you can lookup the possible formatting tokens here:
http://www.cplusplus.com/reference/cstdio/printf/
http://www.cplusplus.com/reference/ctime/strftime/
Just note that the yellow marked tokens usually not work with the C/C++ compiler shipped with QB64.
Title: Re: Help formatting numbers without printing to screen
Post by: TempodiBasic on December 18, 2018, 03:12:27 am
@xra7en

Quote
Here is what I was trying to achieve. Print using ##### etc... works fine, but, it has some padding that I do not want

IMHO this is the same issue
1. about INPUT that cannot take some characters from keyboard or file (think about ASCII table and _MAPUNICODE discussion in other thread)
2. or INPUT that works like an enlarged editor  and it is not possible to select a range (or set) of character to get into as user's input
3. or INPUT that works like an enlarged editor  and it is not possible to define how many characters must be taken form user's input
4. INPUT that works like an enlarged editor  and it let to delete previous characters typed in by user before pressing ENTER key
5. FILES that works fine to show files in the path but its output is only on the screen, and plus in a fixed manner that you cannot modify

and you are right the real solution is to rewrite by your own the function if those of other coders are not suitable for you.

I imagine that the same tip to transform in String the number to print it let you loose some interesting formatting characteristics of PRINT USING , but as you can see it is an old function of BASIC born with the  dark DOS (or before DOS?).

@ Steve
It would be a fine solution to let _OVERWRITE and _INHERITED into BASIC :-)
So I can model KEYWORD to my needs no starting from zero.... following the way of TYPE... END TYPE and DEF FN... END DEF
(how many years do you need to translate actual code into OOP compatible code?)

Thanks to read
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 18, 2018, 01:00:48 pm
I am curious what is the difference with what is wanted here and this:
https://www.qb64.org/forum/index.php?topic=840.msg100462#msg100462


almost nothing, except that I did rewrite that a little cleaner, and now it seems to work, but if you run the code I just posted in a loop, using an increment of - say - 2.4 and deleay it about 1 second or less (there is a reason for that chaos), starting var 10000000.23 (some crazy decimal.. just for use) what I see when I run it, is that around the 1million spot the comma disappears for a second then reappears.

here is a sample you can try
Code: QB64: [Select]
  1. counter = 1
  2. somenumber = 1000000000.23
  3.     LOCATE 1, 1, 0
  4.  
  5.     PRINT somenumber
  6.     somenumber = somenumber + 2.4
  7.  
  8.  
  9.     PRINT num2str$(somenumber, 3) 'use the code function i posted earlier
  10.     _DELAY (1)
  11. LOOP UNTIL counter = 1000
  12.  

the million comma seems to come and go for some reason, and I think it is how I am doing the modulous.(old fashion way vs the "%" method)
Your question is very valid. The only reason I ask is that sometimes my OCD kicks in, and I think, in this case, that I am not seeing the forest for the trees with the PRINT USING format. since it does most the work. And wondering if I am on the right track writing a custom funciton that does almost the same thing.

oooorrrrrr

is there a flag I am missing in the PRINT USING to rid the space padding ?


Title: Re: Help formatting numbers without printing to screen
Post by: SMcNeill on December 18, 2018, 01:14:12 pm
That may be a glitch with the display flickering.

Try adding _DISPLAY to your loop to stop flicker.

If that doesn’t work, put a check to count commas (a DO LOOP with INSTR can do that easily enough), and then pause execution when a value appears without it.  Print that value to the screen, and we can use it to figure out why the comma would appear sometimes, but not others.

Once the fringe case is identified (if it exists), then it can be debugged/accounted for.  ;)
Title: Re: Help formatting numbers without printing to screen
Post by: bplus on December 18, 2018, 02:07:50 pm
Code: QB64: [Select]
  1. DIM somenumber AS DOUBLE '<<<<<<<<<<< added
  2. counter = 1
  3. somenumber = 1000000000.23
  4.     CLS '<<<<<<<<<<<<< added to make sure seeing fresh number clearly
  5.     LOCATE 1, 1, 0 ' <<<< 0 to turn off cursor???
  6.  
  7.  
  8.     'somenumber = somenumber + 2.4
  9.     somenumber = somenumber + RND * 1000 - 500 '<<< to make more interesting
  10.     PRINT somenumber '<<<<<<<<<<<<<<<<< moved to compare equal values
  11.     PRINT xnum2str$(somenumber, 3) 'use the code function i posted earlier  '<<<< added the x in front
  12.     _DELAY (1)
  13. LOOP UNTIL counter = 1000
  14.  
  15.  
  16.  
  17. FUNCTION xnum2str$ (num AS DOUBLE, dec AS INTEGER)
  18.  
  19.     '// convert to number delimted string
  20.  
  21.     DIM number AS STRING
  22.     DIM precision AS STRING
  23.     DIM convert AS STRING
  24.     DIM tempStr AS STRING
  25.     DIM I AS INTEGER
  26.  
  27.     convert = STR$(num) '                                               convert it all to a strig
  28.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  29.     precision = MID$(convert, INSTR(convert, "."), LEN(convert)) '      store the decimal
  30.  
  31.     tempStr = ""
  32.     FOR I = LEN(number) TO 1 STEP -1 '                                  create comma delimted string
  33.         tempStr = MID$(convert, I, 1) + tempStr
  34.         IF I / 3 = INT(I / 3) THEN tempStr = "," + tempStr
  35.     NEXT
  36.     number = tempStr
  37.  
  38.  
  39.     tempStr = "" '                                                      adjust len of precisio to match
  40.     FOR I = 1 TO LEN(precision)
  41.         tempStr = tempStr + MID$(precision, I, 1)
  42.         IF I >= dec THEN EXIT FOR
  43.     NEXT
  44.     precision = tempStr
  45.     xnum2str$ = number + precision
  46.  
  47.  

This looks solid as a rock without _display using QB64 X64. I will complain precision 3, gives only 2 decimal places.

Testing with X32 and negative numbers (dec set at 2), I find error in code:


I also see that my commatose code is not handling neg numbers quite right either. dang!
Title: Re: Help formatting numbers without printing to screen
Post by: freetrav on December 18, 2018, 02:18:28 pm
Just as a random side comment on this subject, I've seen other BASICs with either an SPRINT/SPRINT USING statement (SPRINT -> PRINT to a string) or a USING$ function, which is probably implemented by "riding" the code of PRINT USING. Perhaps it might be worth thinking about implementing _USING$(pattern$,value)?
Title: Re: Help formatting numbers without printing to screen
Post by: SMcNeill on December 18, 2018, 02:44:23 pm
Code: QB64: [Select]
  1. DIM somenumber AS DOUBLE '<<<<<<<<<<< added
  2. counter = 1
  3. somenumber = 1000000000.23
  4.     CLS '<<<<<<<<<<<<< added to make sure seeing fresh number clearly
  5.     LOCATE 1, 1, 0 ' <<<< 0 to turn off cursor???
  6.  
  7.  
  8.     'somenumber = somenumber + 2.4
  9.     somenumber = somenumber + RND * 1000 - 500 '<<< to make more interesting
  10.     PRINT somenumber '<<<<<<<<<<<<<<<<< moved to compare equal values
  11.     PRINT xnum2str$(somenumber, 3) 'use the code function i posted earlier  '<<<< added the x in front
  12.     _DELAY (1)
  13. LOOP UNTIL counter = 1000
  14.  
  15.  
  16.  
  17. FUNCTION xnum2str$ (num AS DOUBLE, dec AS INTEGER)
  18.  
  19.     '// convert to number delimted string
  20.  
  21.     DIM number AS STRING
  22.     DIM precision AS STRING
  23.     DIM convert AS STRING
  24.     DIM tempStr AS STRING
  25.     DIM I AS INTEGER
  26.  
  27.     convert = STR$(num) '                                               convert it all to a strig
  28.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  29.     precision = MID$(convert, INSTR(convert, "."), LEN(convert)) '      store the decimal
  30.  
  31.     tempStr = ""
  32.     FOR I = LEN(number) TO 1 STEP -1 '                                  create comma delimted string
  33.         tempStr = MID$(convert, I, 1) + tempStr
  34.         IF I / 3 = INT(I / 3) THEN tempStr = "," + tempStr
  35.     NEXT
  36.     number = tempStr
  37.  
  38.  
  39.     tempStr = "" '                                                      adjust len of precisio to match
  40.     FOR I = 1 TO LEN(precision)
  41.         tempStr = tempStr + MID$(precision, I, 1)
  42.         IF I >= dec THEN EXIT FOR
  43.     NEXT
  44.     precision = tempStr
  45.     xnum2str$ = number + precision
  46.  
  47.  

This looks solid as a rock without _display using QB64 X64. I will complain precision 3, gives only 2 decimal places.

Testing with X32 and negative numbers (dec set at 2), I find error in code:


I also see that my commatose code is not handling neg numbers quite right either. dang!

Just check for negative values.  If found, use ABS() on the number, generate the result, and then add a “-“ to the end result.  ;)
Title: Re: Help formatting numbers without printing to screen
Post by: bplus on December 18, 2018, 02:52:45 pm
Yeah I have it fixed in commatose:
Code: QB64: [Select]
  1. DIM somenumber AS DOUBLE '<<<<<<<<<<< added
  2. counter = 1
  3. somenumber = 0
  4.     CLS '<<<<<<<<<<<<< added to make sure seeing fresh number clearly
  5.     LOCATE 1, 1, 0 ' <<<< 0 to turn off cursor???
  6.  
  7.     'somenumber = somenumber + 2.4
  8.     somenumber = somenumber - RND * 10 + 4 '<<< to make more interesting  drift down
  9.     PRINT "               PRINT somenumber: "; somenumber '<<<<<<<<<<<<<<<<< moved to compare equal values
  10.     PRINT " PRINT xnum2str$(somenumber, 3): "; xnum2str$(somenumber, 3) 'use the code function i posted earlier  '<<<< added the x in front
  11.     PRINT "PRINT Commatose$(somenumber, 3): "; Commatose$(somenumber, 3)
  12.     _LIMIT 60
  13. LOOP UNTIL counter = 1000
  14.  
  15.  
  16.  
  17. FUNCTION xnum2str$ (num AS DOUBLE, dec AS INTEGER)
  18.  
  19.     '// convert to number delimted string
  20.  
  21.     DIM number AS STRING
  22.     DIM precision AS STRING
  23.     DIM convert AS STRING
  24.     DIM tempStr AS STRING
  25.     DIM I AS INTEGER
  26.  
  27.     convert = STR$(num) '                                               convert it all to a strig
  28.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  29.     precision = MID$(convert, INSTR(convert, "."), LEN(convert)) '      store the decimal
  30.  
  31.     tempStr = ""
  32.     FOR I = LEN(number) TO 1 STEP -1 '                                  create comma delimted string
  33.         tempStr = MID$(convert, I, 1) + tempStr
  34.         IF I / 3 = INT(I / 3) THEN tempStr = "," + tempStr
  35.     NEXT
  36.     number = tempStr
  37.  
  38.  
  39.     tempStr = "" '                                                      adjust len of precisio to match
  40.     FOR I = 1 TO LEN(precision)
  41.         tempStr = tempStr + MID$(precision, I, 1)
  42.         IF I >= dec THEN EXIT FOR
  43.     NEXT
  44.     precision = tempStr
  45.     xnum2str$ = number + precision
  46.  
  47.  
  48. FUNCTION Commatose$ (value AS DOUBLE, precision AS INTEGER)
  49.     DIM v AS DOUBLE
  50.     v = value 'copy so it doesn't get changed
  51.     IF v < 0 THEN s$ = "-": v = v * -1 ELSE s$ = ""
  52.  
  53.     sv$ = LTRIM$(STR$(v)) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ltrim$ remove leading space!
  54.     dot = INSTR(sv$, ".")
  55.     IF dot THEN
  56.         i$ = MID$(sv$, 1, dot - 1)
  57.         d$ = LEFT$(MID$(sv$, dot + 1) + STRING$(precision, "0"), precision)
  58.     ELSE
  59.         i$ = sv$
  60.         d$ = STRING$(precision, "0")
  61.     END IF
  62.     FOR i = LEN(i$) TO 1 STEP -1
  63.         c = c + 1
  64.         IF c = 4 THEN c = 1: b$ = "," + b$
  65.         b$ = MID$(i$, i, 1) + b$
  66.     NEXT
  67.     Commatose$ = s$ + b$ + "." + d$
  68.  
  69.  
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 18, 2018, 04:58:37 pm
That may be a glitch with the display flickering.

Try adding _DISPLAY to your loop to stop flicker.

If that doesn’t work, put a check to count commas (a DO LOOP with INSTR can do that easily enough), and then pause execution when a value appears without it.  Print that value to the screen, and we can use it to figure out why the comma would appear sometimes, but not others.

Once the fringe case is identified (if it exists), then it can be debugged/accounted for.  ;)

you might be on to something...
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 18, 2018, 05:03:24 pm
btw, after reading this and my otherone - they are extremely similar, it is the same issue, with a sleight variation - any chance a mod can move this to that thread. Makes more sense.

I think I just solved it too, it is something to do with the way I am parsing the string

using anything larger than 100k or 1mil the comma drifts. while I will look here in a sec, but for public curiosity, is qb64 strings zero based or 1?

abcdef
does "a" position = zero or 1?

Title: Re: Help formatting numbers without printing to screen
Post by: SMcNeill on December 18, 2018, 05:06:49 pm
btw, after reading this and my otherone - they are extremely similar, it is the same issue, with a sleight variation - any chance a mod can move this to that thread. Makes more sense.

I think I just solved it too, it is something to do with the way I am parsing the string

using anything larger than 100k or 1mil the comma drifts. while I will look here in a sec, but for public curiosity, is qb64 strings zero based or 1?

abcdef
does "a" position = zero or 1?

1st  position
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 18, 2018, 08:38:39 pm
OK. works!! Got it. Woot


Code: QB64: [Select]
  1. FUNCTION num2str$ (num AS DOUBLE, dec AS INTEGER)
  2.  
  3.     '// convert to number delimted string
  4.  
  5.     DIM number AS STRING '                                              stores whole number(w/o dec) minus "."
  6.     DIM precision AS STRING '                                           stores decimals (minus ".")
  7.     DIM convert AS STRING '                                             stores the orginal number in string format
  8.     DIM tempStr AS STRING '                                             used to create the new formated string
  9.     DIM I, j AS INTEGER '                                               i=loop counter, j= delimeter counter
  10.  
  11.     convert = STR$(num) '                                               convert it all to a strig
  12.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  13.     precision = MID$(convert, INSTR(convert, ".") + 1, LEN(convert)) '  store the decimal
  14.     number = RTRIM$(LTRIM$(number)) '                                   trim it up
  15.    
  16.        IF num < 1000 THEN '                                                if its less than 1000, then no comma needed
  17.         num2str$ = number + "." + LEFT$(precision, dec) '               assign function for dec (precision)
  18.         EXIT SUB '                                                      leave this sub now
  19.     END IF
  20.  
  21.  
  22.  
  23.  
  24.     j = 1 '                                                             set delimeter counter
  25.     tempStr = ""
  26.     FOR I = LEN(number) TO 1 STEP -1 '                                  count from the right to left of number
  27.  
  28.         IF j > 3 THEN '                                                 if delimeter counter is > 3 then
  29.             tempStr = "," + tempStr '                                   add a comma
  30.             j = 1 '                                                     reset the delimeter counter
  31.         END IF
  32.  
  33.  
  34.         tempStr = MID$(number, I, 1) + tempStr '                        add the next digit from "number" to tempstr
  35.         j = j + 1 '                                                     increase the delimeter counter
  36.  
  37.     NEXT '                                                              keep looping
  38.     number = tempStr '                                                  reassign number to the actual string created
  39.  
  40.  
  41.     tempStr = "" '                                                      clear tempStr so we can make the percision
  42.     FOR I = 1 TO LEN(precision) '                                       start looping through dec
  43.         tempStr = tempStr + MID$(precision, I, 1) '                     create it
  44.         IF I >= dec THEN EXIT FOR '                                     is it the number of places(dec) we want? exit this loop
  45.     NEXT
  46.     precision = tempStr '                                               assign the precision var to the newly created decimal length
  47.     num2str$ = number + "." + precision '                               return it back as a fll string with "."
  48.  
  49.  
  50.  

the only way I could get this to work was to use the "J" loop.
I tried the MOD, and I tried an old method of

Code: QB64: [Select]
  1. if i/3 = int(i/3) then add a comma

none of them worked.
This does.
Returns a comma delimited string variable from an integer or double

Opinions? Suggestions?

I had to stop looking at my Steam Que so I could focus on this LOL...
Title: Re: Help formatting numbers without printing to screen
Post by: Fifi on December 19, 2018, 05:11:13 am
Just as a random side comment on this subject, I've seen other BASICs with either an SPRINT/SPRINT USING statement (SPRINT -> PRINT to a string) or a USING$ function, which is probably implemented by "riding" the code of PRINT USING. Perhaps it might be worth thinking about implementing _USING$(pattern$,value)?

As another side comment: would it be so difficult to interface (implement) two new reserved words with the C/C++ printf() and sprintf() stdio.h functions using their syntax and parameters that would avoid to "translate" the current PRINT USING while being more powerfull?

Just my two cents.
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 19, 2018, 07:09:08 am
Just as a random side comment on this subject, I've seen other BASICs with either an SPRINT/SPRINT USING statement (SPRINT -> PRINT to a string) or a USING$ function, which is probably implemented by "riding" the code of PRINT USING. Perhaps it might be worth thinking about implementing _USING$(pattern$,value)?

As another side comment: would it be so difficult to interface (implement) two new reserved words with the C/C++ printf() and sprintf() stdio.h functions using their syntax and parameters that would avoid to "translate" the current PRINT USING while being more powerfull?

Just my two cents.

hey, every penny counts :-)
I used to work with C++/# in my old BBS days. Wish I would have stuck with it, because I went from Telegard->Invision-Infinity (all pascal) to Proboard(C++), very weird change. Not only that, with all the pascal making a hit, I thought that would be the language of choice, and BOOM here comes "C" LOL

OK I digress.....

sprint, print etc.. all output to the screen/console

I needed something that returned a var that does the same thing.
or at minimum a PHP version of "number_format (http://php.net/manual/en/function.number-format.php)" which does not require a template. Actually, I should check that script source out and see how they did that (slow thinker LOL)

I do like your idea, however Im wanting to keep the langs separate (at least for now).

Nice Idea though

EDIT:
================================

Here is afunctional version in PHP... The original is in C, So will look at that. I personally think this might be a useful function to add in QB64 IMHO
Code: QB64: [Select]
  1. function number_format($num){
  2.   $num=(string)$num;
  3.   $len=strlen($num);
  4.   $newnum='';
  5.   for ($i=0; $i<$len; ++$i) {
  6.     if (($i%3==0) && $i) {
  7.       $newnum=','.$newnum;
  8.     }
  9.     $newnum=$num[$len-$i-1].$newnum;
  10.   }
  11.   return $newnum;
  12. }
Title: Re: Help formatting numbers without printing to screen
Post by: TempodiBasic on December 19, 2018, 07:42:47 am
Hi
fine to add meself to this thread again

I have wrote a my routine... :-)
yes! Why? As someone knows the way to write numbers in italian is different from that used here, (also date and some few other things...) so I try to make my algorithm in QB64 working on string format and letting to show international format (, for thousand and . for decimal or that italian that is the inverse system  i.e.  international 1,000,009.999 = italian 1.000.009,999 ). So here my work.

I take your code and add my function.
PS: Just modify the number maker to have greater numbers... and both negative and positive numbers...on program's controls you can exit by keyboard and stop and go by space/anyOtherKey.

Attached are the  images of results...


as you can see in image3
1.  I am not able to manage Commatose$'s output, I have got a flickering passing from negative to positive numbers...
2. xnum2str$ have a bug...

Thanks to read and to reply

PS I have no knowledge in the matter please tell me, but after the decimal point do the digits need no formatting with separator ?


PSS sorry I have forgotten to post the code....
Code: QB64: [Select]
  1. DIM somenumber AS DOUBLE '<<<<<<<<<<< added
  2. DIM counter AS INTEGER, signs AS INTEGER
  3.  
  4. counter = 1
  5. somenumber = 0
  6.     CLS '<<<<<<<<<<<<< added to make sure seeing fresh number clearly
  7.     LOCATE 1, 1, 0 ' <<<< 0 to turn off cursor???
  8.     IF (RND * 5 + 1) > 3 THEN signs = -1 ELSE signs = 1
  9.  
  10.     somenumber = (somenumber - (RND * 10) + 40000000) * signs
  11.     PRINT "                    PRINT somenumber: "; somenumber
  12.     PRINT " PRINT xnum2str$(somenumber, 3):      "; xnum2str$(somenumber, 3)
  13.     PRINT " PRINT num2str$(somenumber, 3):       "; num2str$(somenumber, 3)
  14.     PRINT "PRINT Commatose$(somenumber, 3):       "; Commatose$(somenumber, 3)
  15.     PRINT "PRINT MyNumToString$(somenumber, 3, 1)"; MyNumToString$(somenumber, 3, 1)
  16.     PRINT "PRINT MyNumToString$(somenumber, 3, 2)"; MyNumToString$(somenumber, 3, 2)
  17.     _LIMIT 60
  18.     IF _KEYDOWN(32) THEN SLEEP
  19.  
  20.  
  21.  
  22. FUNCTION MyNumToString$ (num AS DOUBLE, dec AS INTEGER, mode AS INTEGER)
  23.     DIM Thousand AS STRING * 1, Decimal AS STRING * 1, NumberStringed AS STRING
  24.     DIM DecimalString AS STRING, NumberString AS STRING, Minus AS STRING * 1
  25.     DIM DecimalPoint AS INTEGER, LenghtDecimal AS INTEGER, LenghtInteger AS INTEGER
  26.     DIM Starter AS INTEGER, Counter AS INTEGER, Steps AS INTEGER
  27.     IF mode = 1 THEN
  28.         ' english puntaction for number
  29.         Thousand = ","
  30.         Decimal = "."
  31.     ELSEIF mode = 2 THEN
  32.         ' italian puntaction for number
  33.         Thousand = "."
  34.         Decimal = ","
  35.     ELSE
  36.         PRINT "invalid Mode setting"
  37.         EXIT FUNCTION
  38.     END IF
  39.     Minus = ""
  40.     NumberStringed = LTRIM$(STR$(num))
  41.     IF (num < 0) AND INSTR(NumberStringed, "-") = 1 THEN
  42.         NumberStringed = MID$(NumberStringed, 2)
  43.         Minus = "-"
  44.     END IF
  45.     DecimalPoint = INSTR(NumberStringed, ".")
  46.     DecimalString = RIGHT$(NumberStringed, LEN(NumberStringed) - DecimalPoint)
  47.     LenghtDecimal = LEN(RIGHT$(NumberStringed, LEN(NumberStringed) - DecimalPoint))
  48.     NumberString = LEFT$(NumberStringed, DecimalPoint - 1)
  49.     LenghtInteger = LEN(NumberString)
  50.  
  51.     IF (LenghtInteger < 4) THEN
  52.         MyNumToString$ = Minus + NumberString + Decimal + LEFT$(DecimalString, dec)
  53.     ELSE
  54.         NumberStringed = ""
  55.         Starter = (LenghtInteger MOD 3)
  56.         Steps = INT(LenghtInteger / 3)
  57.         IF Starter > 0 THEN NumberStringed = LEFT$(NumberString, Starter) + Thousand
  58.         FOR Counter = 0 TO Steps - 1
  59.             NumberStringed = NumberStringed + MID$(NumberString, Starter + (Counter * 3) + 1, 3) + Thousand
  60.         NEXT
  61.         NumberStringed = LEFT$(NumberStringed, LEN(NumberStringed) - 1)
  62.         MyNumToString$ = Minus + NumberStringed + Decimal + LEFT$(DecimalString, dec)
  63.     END IF
  64.  
  65.  
  66. FUNCTION num2str$ (num AS DOUBLE, dec AS INTEGER)
  67.  
  68.     '// convert to number delimted string
  69.  
  70.     DIM number AS STRING '                                              stores whole number(w/o dec) minus "."
  71.     DIM precision AS STRING '                                           stores decimals (minus ".")
  72.     DIM convert AS STRING '                                             stores the orginal number in string format
  73.     DIM tempStr AS STRING '                                             used to create the new formated string
  74.     DIM I, j AS INTEGER '                                               i=loop counter, j= delimeter counter
  75.  
  76.     convert = STR$(num) '                                               convert it all to a strig
  77.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  78.     precision = MID$(convert, INSTR(convert, ".") + 1, LEN(convert)) '  store the decimal
  79.     number = RTRIM$(LTRIM$(number)) '                                   trim it up
  80.  
  81.     IF num < 1000 THEN '                                                if its less than 1000, then no comma needed
  82.         num2str$ = number + "." + LEFT$(precision, dec) '               assign function for dec (precision)
  83.         EXIT SUB '                                                      leave this sub now
  84.     END IF
  85.  
  86.  
  87.  
  88.  
  89.     j = 1 '                                                             set delimeter counter
  90.     tempStr = ""
  91.     FOR I = LEN(number) TO 1 STEP -1 '                                  count from the right to left of number
  92.  
  93.         IF j > 3 THEN '                                                 if delimeter counter is > 3 then
  94.             tempStr = "," + tempStr '                                   add a comma
  95.             j = 1 '                                                     reset the delimeter counter
  96.         END IF
  97.  
  98.  
  99.         tempStr = MID$(number, I, 1) + tempStr '                        add the next digit from "number" to tempstr
  100.         j = j + 1 '                                                     increase the delimeter counter
  101.  
  102.     NEXT '                                                              keep looping
  103.     number = tempStr '                                                  reassign number to the actual string created
  104.  
  105.  
  106.     tempStr = "" '                                                      clear tempStr so we can make the percision
  107.     FOR I = 1 TO LEN(precision) '                                       start looping through dec
  108.         tempStr = tempStr + MID$(precision, I, 1) '                     create it
  109.         IF I >= dec THEN EXIT FOR '                                     is it the number of places(dec) we want? exit this loop
  110.     NEXT
  111.     precision = tempStr '                                               assign the precision var to the newly created decimal length
  112.     num2str$ = number + "." + precision '                               return it back as a fll string with "."
  113.  
  114.  
  115. FUNCTION xnum2str$ (num AS DOUBLE, dec AS INTEGER)
  116.  
  117.     '// convert to number delimted string
  118.  
  119.     DIM number AS STRING
  120.     DIM precision AS STRING
  121.     DIM convert AS STRING
  122.     DIM tempStr AS STRING
  123.     DIM I AS INTEGER
  124.  
  125.     convert = STR$(num) '                                               convert it all to a strig
  126.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  127.     precision = MID$(convert, INSTR(convert, "."), LEN(convert)) '      store the decimal
  128.  
  129.     tempStr = ""
  130.     FOR I = LEN(number) TO 1 STEP -1 '                                  create comma delimted string
  131.         tempStr = MID$(convert, I, 1) + tempStr
  132.         IF I / 3 = INT(I / 3) THEN tempStr = "," + tempStr
  133.     NEXT
  134.     number = tempStr
  135.  
  136.  
  137.     tempStr = "" '                                                      adjust len of precisio to match
  138.     FOR I = 1 TO LEN(precision)
  139.         tempStr = tempStr + MID$(precision, I, 1)
  140.         IF I >= dec THEN EXIT FOR
  141.     NEXT
  142.     precision = tempStr
  143.     xnum2str$ = number + precision
  144.  
  145.  
  146. FUNCTION Commatose$ (value AS DOUBLE, precision AS INTEGER)
  147.     DIM v AS DOUBLE
  148.     v = value 'copy so it doesn't get changed
  149.     IF v < 0 THEN s$ = "-": v = v * -1 ELSE s$ = ""
  150.  
  151.     sv$ = LTRIM$(STR$(v)) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ltrim$ remove leading space!
  152.     dot = INSTR(sv$, ".")
  153.     IF dot THEN
  154.         i$ = MID$(sv$, 1, dot - 1)
  155.         d$ = LEFT$(MID$(sv$, dot + 1) + STRING$(precision, "0"), precision)
  156.     ELSE
  157.         i$ = sv$
  158.         d$ = STRING$(precision, "0")
  159.     END IF
  160.     FOR i = LEN(i$) TO 1 STEP -1
  161.         c = c + 1
  162.         IF c = 4 THEN c = 1: b$ = "," + b$
  163.         b$ = MID$(i$, i, 1) + b$
  164.     NEXT
  165.     Commatose$ = s$ + b$ + "." + d$
  166.  
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 19, 2018, 08:14:55 am
Looks very nice - AND you seem to have a dilemma that I have. those darn leading spaces for digits that do not fill it.
Thus why I tried my hand at making a custom function,

According to the definition  (https://qb64.org/wiki/PRINT_USING)- if I read it right:

Quote
  #    Denotes a numerical digit. An appropriate number of digits should be used for values received.

so you kinda have to guess the amount of digits  on a dynamic application. 

HOWEVER FreeTrav gave me an Idea (sorta my original question about using the "PRINT USING" to do the brunt of the work, and I came up with this:
It seems to solve the goofy leading spaces for digits that are not there. It adds a hash for each digit dynamically THEN IT USES THE PRINT USING
Code: QB64: [Select]
  1. SUB number_format (num AS DOUBLE, flt AS INTEGER)
  2.     DIM number AS STRING '                                              stores whole number(w/o dec) minus "."
  3.     DIM convert AS STRING '                                             stores the orginal number in string format
  4.     DIM I AS INTEGER '                                                  i=loop counter delimeter counter
  5.     DIM tpl AS STRING '                                                 .. using template
  6.  
  7.     convert = STR$(num) '                                               convert it all to a strig
  8.     IF RIGHT$(convert, 1) <> "." THEN convert = convert + "." '         weird fix for lower numbers
  9.     number = LEFT$(convert, INSTR(convert, ".") - 1) '                  store the integer
  10.     number = RTRIM$(LTRIM$(number)) '                                   trim it up
  11.  
  12.     tpl = "" '                                                          create a template string for USING
  13.  
  14.     FOR I = 1 TO LEN(number): tpl = tpl + "#": NEXT '                   add a hash per digit per integer digit
  15.     tpl = tpl + "," '                                                   set the comma tag
  16.     IF flt > 0 THEN '                                                   is the decimal count set?
  17.         tpl = tpl + "." '                                               add the decimal tag
  18.         FOR I = 1 TO flt: tpl = tpl + "#": NEXT '                       how many places (as per dec)
  19.     END IF
  20.     '    PRINT "tpl for "; num
  21.     '    PRINT tpl
  22.     PRINT USING tpl; num '                                              now PRINT USING should look cleaner
  23.  

So now instead of "re-inventing the wheel" I just changed it a little. Now there are no spaces, the decimal and digits can grow dynamically

whatcha think? ya?
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 19, 2018, 08:17:50 am
Hi
fine to add meself to this thread again

I have wrote a my routine... :-)
yes! Why? As someone knows the way to write numbers in italian is different from that used here, (also date and some few other things...) so I try to make my algorithm in QB64 working on string format and letting to show international format (, for thousand and . for decimal or that italian that is the inverse system  i.e.  international 1,000,009.999 = italian 1.000.009,999 ). So here my work.


That is very interesting, If I understand...
Italian uses a ","(comma) for a decimal notation and a "."(dot) for thousands separator?
If so we could change the function to ask for a delimiter.
 
thanks for that info.
Title: Re: Help formatting numbers without printing to screen
Post by: TempodiBasic on December 19, 2018, 09:32:09 am

Going in depth with separator symbol for thousand and for decimal I find a very various situation in the world...
so my function with a Mode can be expanded to choose national writing mode for numbers...

see here how many kind of ways to write the same number....
https://en.wikipedia.org/wiki/Decimal_separator (https://en.wikipedia.org/wiki/Decimal_separator)

Quote
The following examples show the decimal separator and the thousands separator in various countries that use the Arabic numeral system.

Style   Countries
1,234,567.89   Canada (English-speaking; unofficial), China, Hong Kong, Ireland, Israel, Japan, Korea, Malaysia, México, New Zealand, Pakistan, Philippines, Singapore, Taiwan, Thailand, United Kingdom, United States.
1234567.89   SI style (English version), Australia, Canada (English-speaking), China, Sri Lanka, Switzerland (officially encouraged for currency numbers only[40]).
1234567,89   SI style (French version), Albania, Belgium (French), Bulgaria, Canada (French-speaking), Czech Republic, Estonia, Finland, France, Hungary, Italy, Kosovo, Latin Europe, Norway, Peru, Poland, Lithuania, Russia, Slovakia, Slovenia, South Africa, Sweden, Switzerland (officially encouraged, except currency numbers[40]), Ukraine.
1,234,567·89   Ireland, Malaysia, Malta, Philippines, Singapore, Taiwan, United Kingdom (older, typically hand written)[41]
1.234.567,89   Argentina, Austria, Belgium (Dutch), Bosnia and Herzegovina, Brazil, Chile, Costa Rica, Croatia,[42][43] Denmark, Germany, Greece, Indonesia, Italy, Netherlands, Portugal, Romania, Russia, Slovenia, Spain,[44]Turkey, Vietnam.
12,34,567.89   Bangladesh, India (see Indian Numbering System).
1'234'567.89   Switzerland (computing), Liechtenstein.
1'234'567,89   Switzerland (handwriting).
1.234.567'89   Spain (handwriting).
123,4567.89   China (based on powers of 10 000—see Chinese numerals).
or you can expand your function to get Universal goal of accurate annotation for numbers...

so you can candidate your function for QB64 library  (a package must respect local convention... IMHO)
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 19, 2018, 10:32:49 am
one sec..
I have to move this rock off me so I can see....
Title: Re: Help formatting numbers without printing to screen
Post by: jack on December 19, 2018, 07:14:14 pm
I wrote one exactly like that description but it is currently being held hostage in the lost realm of our old forum. Bummer.
unfortunately the wayback machine only shows the thread title in Libraries Format$() - or "PRINT USING for variables"
Title: Re: Help formatting numbers without printing to screen
Post by: Fifi on December 19, 2018, 07:40:26 pm
That is very interesting, If I understand...
Italian uses a ","(comma) for a decimal notation and a "."(dot) for thousands separator?
If so we could change the function to ask for a delimiter.
 
thanks for that info.

French do so too (optionnaly using dots to separate the different float parts by group of 3 or not, and always using a comma to separate the decimal part).

e.g. both 1234567.89 and 1.234.567,89 are valid french notations.

Not sure but I think most european countries do so too, but of course England (that is not really a European country especially since their Brexit).

Meanwhile, I still think interfacing the C/C++ printf(), fprintf(), sprintf() and all the other input/output functions of the stdio.h library would be the easiest and best solution.

I don't know how hard it would be to do it since QB64 translates .bas code in .cpp code.

So, why not using _PRINTF(), _SPRINTF(), _FPRINTF() etc. with all their relevant parameters that also take care of the right or left aligment as well as leading spaces?

Just my two cents.
Title: Re: Help formatting numbers without printing to screen
Post by: FellippeHeitor on December 19, 2018, 07:54:15 pm
I wrote one exactly like that description but it is currently being held hostage in the lost realm of our old forum. Bummer.
unfortunately the wayback machine only shows the thread title in Libraries Format$() - or "PRINT USING for variables"

That was exactly it.
Title: Re: Help formatting numbers without printing to screen
Post by: xra7en on December 21, 2018, 10:27:18 am

So, why not using _PRINTF(), _SPRINTF(), _FPRINTF() etc. with all their relevant parameters that also take care of the right or left aligment as well as leading spaces?

Just my two cents.

simple answer - just trying to stay "native" :-)
and IMHO, I only use QB64 for basic / fun programing, I have my heavy hitter languages for the Clydesdale work :-)
Title: Re: Help formatting numbers without printing to screen
Post by: Fifi on December 21, 2018, 06:13:48 pm
Hi xra7en


So, why not using _PRINTF(), _SPRINTF(), _FPRINTF() etc. with all their relevant parameters that also take care of the right or left aligment as well as leading spaces?

Just my two cents.

simple answer - just trying to stay "native" :-)
and IMHO, I only use QB64 for basic / fun programing, I have my heavy hitter languages for the Clydesdale work :-)

Sure, you can both be "native" and use QB64 for fun.

I don't have a problem with that.

However, since:

a) QB64 isn't a 100 per 100 full compatible QB4.5 product "yet" (and obviously never will be);
b) Nicely extend the language with a lot of new functions,
c) Is able to include native C/C++ libraries,
d) and generate C++ code...

why not extend it with new native stdio.h C/C++ functions with dedicated names (e.g. _PRINTF(), _SPRINTF(), etc)?

You will not be obliged to use such extended functions that, however could solve problems to others trying different solutions.

The perfect exemple are the printf(), sprintf(), etc. functions that provides responses that the "basic" PRINT USING does not.

However, since QB64 can include native C/C++ libraries, I'm still expecting to undersand how to "include" the stdio.h standard input/output library with a code sample, a question that noboby never ever responded to for over 2 years.

So, is QB64 really capable to include such a so basic and standard C/C++ library?

If yes, PLEASE show me how to do so.

Just my two cents.

Cheers.
Fifi