Author Topic: PRINT USING for non US numbers  (Read 7172 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: PRINT USING for non US numbers
« Reply #15 on: January 02, 2021, 06:20:16 pm »
Wouldn't an easy solution here be to just print to a temp screen, read the output into a string, and then swap the periods and commas?

Here's a format$ routine that I worked up in the past.  All it needs is to swap periods for commas, correct?

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
« Last Edit: January 02, 2021, 06:23:28 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: PRINT USING for non US numbers
« Reply #16 on: January 02, 2021, 07:44:45 pm »
Here's the new IndexFomat$() function, which I've mentioned earlier:
https://www.qb64.org/forum/index.php?topic=2932.0
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 bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #17 on: January 02, 2021, 08:09:00 pm »
Wouldn't an easy solution here be to just print to a temp screen, read the output into a string, and then swap the periods and commas?

Here's a format$ routine that I worked up in the past.  All it needs is to swap periods for commas, correct?

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


Oh Steve added code to his post since I last looked. In meantime I worked up complete conversion starting with Fellippe's format$ function which is a little buggy, fixed the ones that started with comma but starting with zero's is more work!
Code: QB64: [Select]
  1. _TITLE "Format$ Function by Fellippe"
  2. ' ref  https://www.qb64.org/forum/index.php?topic=2932.msg121943#msg121943
  3.     r = RND < .5
  4.     IF r THEN x## = -1 * RND * 10 ^ INT(RND * 9) ELSE x## = RND * 10 ^ INT(RND * 9)
  5.     x## = x## + RND
  6.     PRINT swapAB$(format$("#####################,.####", x##), ",", ".")
  7.     SLEEP
  8.  
  9. FUNCTION format$ (template$, value##) 'this is not perfect some start with 0's
  10.     DIM tempImage&, prevDest&, prevSource&
  11.     DIM i AS INTEGER, result$
  12.  
  13.     tempImage& = _NEWIMAGE(LEN(template$) + 10, 1, 0)
  14.  
  15.     prevDest& = _DEST
  16.     prevSource& = _SOURCE
  17.     _DEST tempImage&
  18.     _SOURCE tempImage&
  19.  
  20.     PRINT USING template$; value##;
  21.     FOR i = 1 TO POS(0)
  22.         result$ = result$ + CHR$(SCREEN(1, i))
  23.     NEXT
  24.  
  25.     _DEST prevDest&
  26.     _SOURCE prevSource&
  27.     _FREEIMAGE tempImage&
  28.     IF LEFT$(result$, 1) = "," THEN result$ = RIGHT$(result$, LEN(result$) - 1) ' fix results that start with comma
  29.     format$ = result$
  30.  
  31. FUNCTION swapAB$ (s$, A$, B$)
  32.     FOR i = 1 TO LEN(s$)
  33.         IF MID$(s$, i, 1) = A$ THEN
  34.             build$ = build$ + B$
  35.         ELSEIF MID$(s$, i, 1) = B$ THEN
  36.             build$ = build$ + A$
  37.         ELSE
  38.             build$ = build$ + MID$(s$, i, 1)
  39.         END IF
  40.     NEXT
  41.     swapAB$ = build$
  42.  
  43.  
  44.  

Perhaps someone who actually needs this will fix remainder of bugs.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #18 on: January 02, 2021, 08:31:04 pm »
OK this is slightly better, no problem with 0 starts except where they should be, is it the string parameter?
I bet, because then you dont have to worry about the dang Number type!

Code: QB64: [Select]
  1. _TITLE "Format$ Function by Steve best so far 2021-01-02" ' b+ mod 2021-01-02
  2. ' ref https://www.qb64.org/forum/index.php?topic=3426.msg127488#msg127488
  3.     r = RND < .5
  4.     IF r THEN x## = -1 * RND * 10 ^ INT(RND * 9) ELSE x## = RND * 10 ^ INT(RND * 9)
  5.     x## = x## + RND
  6.     PRINT swapAB$(format$("#####################,.####", STR$(x##)), ",", ".")
  7.     SLEEP
  8.  
  9. FUNCTION format$ (template AS STRING, text AS STRING)
  10.     d = _DEST: s = _SOURCE
  11.     n = _NEWIMAGE(80, 80, 0)
  12.     _DEST n: _SOURCE n
  13.     PRINT USING template; VAL(text)
  14.     FOR i = 1 TO 79
  15.         t$ = t$ + CHR$(SCREEN(1, i))
  16.     NEXT
  17.     IF LEFT$(t$, 1) = "%" THEN t$ = MID$(t$, 2)
  18.     format$ = t$
  19.     _DEST d: _SOURCE s
  20.     _FREEIMAGE n
  21.  
  22. FUNCTION swapAB$ (s$, A$, B$)
  23.     FOR i = 1 TO LEN(s$)
  24.         IF MID$(s$, i, 1) = A$ THEN
  25.             build$ = build$ + B$
  26.         ELSEIF MID$(s$, i, 1) = B$ THEN
  27.             build$ = build$ + A$
  28.         ELSE
  29.             build$ = build$ + MID$(s$, i, 1)
  30.         END IF
  31.     NEXT
  32.     swapAB$ = build$
  33.  
  34.  
  35.  
« Last Edit: January 02, 2021, 08:33:59 pm by bplus »

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: PRINT USING for non US numbers
« Reply #19 on: January 03, 2021, 04:55:13 am »
Wouldn't an easy solution here be to just print to a temp screen, read the output into a string, and then swap the periods and commas?

Here's a format$ routine that I worked up in the past.  All it needs is to swap periods for commas, correct?


Thanks for showing this code, I wasn't familiar with the posibility of temp screens.  This may open a lot of new ideas as your examples shows.  I am going to play with this one!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #20 on: January 03, 2021, 11:21:31 am »
Thanks for showing this code, I wasn't familiar with the posibility of temp screens.  This may open a lot of new ideas as your examples shows.  I am going to play with this one!

Yes, an eye opener to me when I first came to QB64! from QB4.5 and early VB.

Screen 0 I think is the only one you can pull out characters on screen with SCREEN() Function.
« Last Edit: January 03, 2021, 11:24:10 am by bplus »

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: PRINT USING for non US numbers
« Reply #21 on: January 03, 2021, 11:33:37 am »
It is the only screen I use,  graphics are not for me.
Only wishing it was somewhat more flexiible, bigger, resizeable, copy text from it (in win ctrl-C) ...

I'm still thinking I haven't seen all new goodies from QB64, compared to QB4.5! ;-)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #22 on: January 03, 2021, 12:58:09 pm »
It is the only screen I use,  graphics are not for me.
Only wishing it was somewhat more flexiible, bigger, resizeable, copy text from it (in win ctrl-C) ...

I'm still thinking I haven't seen all new goodies from QB64, compared to QB4.5! ;-)

screen0 = 0 ' no graphics
anySize& = _NEWIMAGE(anyWidth, anyHeight, screen0 )

_dest anySize&  'private area for working on screen stuff or whatever

_dest 0 'back to the screen for display to user or can
Screen anySize&
« Last Edit: January 03, 2021, 01:01:37 pm by bplus »

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: PRINT USING for non US numbers
« Reply #23 on: January 03, 2021, 02:53:06 pm »
Amazing!  But so offtopic ;-)   I've got it working and will play around with this.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: PRINT USING for non US numbers
« Reply #24 on: January 03, 2021, 02:54:40 pm »
Amazing!  But so offtopic ;-)   I've got it working and will play around with this.

You might also try adding a $RESIZE command to your code.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: PRINT USING for non US numbers
« Reply #25 on: January 03, 2021, 03:04:42 pm »
You might also try adding a $RESIZE command to your code.  ;)
Noted! :-)   I know my way around on the Wiki but do not always see the connection between the (new) commands.
Also, it sometimes takes some puzzling to see how the system 'thinks' (like with the screen and _dest commands). Before this I only discovered the Console as extra window, because of it's different properties (more shell window like, just like QB4.5).