Author Topic: Help formatting numbers without printing to screen  (Read 10452 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Help formatting numbers without printing to screen
« on: December 17, 2018, 04:15:53 pm »
is there a way to do the following (pseudo code)

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

print var
« Last Edit: December 17, 2018, 04:20:35 pm by odin »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline odin

  • Administrator
  • Newbie
  • Posts: 92
  • I am.
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #1 on: December 17, 2018, 04:21:09 pm »
This topic has been renamed to properly summarize the query it contains.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #2 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


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: Help formatting numbers without printing to screen
« Reply #3 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.

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Help formatting numbers without printing to screen
« Reply #4 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.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #5 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

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
Programming isn't difficult, only it's  consuming time and coffee

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #6 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.  
« Last Edit: December 17, 2018, 07:13:53 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #7 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
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #8 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
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: Help formatting numbers without printing to screen
« Reply #9 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #10 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)
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: Help formatting numbers without printing to screen
« Reply #11 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
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #12 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.  
« Last Edit: December 18, 2018, 01:36:09 am by bplus »

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #13 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.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Help formatting numbers without printing to screen
« Reply #14 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
Programming isn't difficult, only it's  consuming time and coffee