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

0 Members and 1 Guest are viewing this topic.

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
PRINT USING for non US numbers
« on: December 29, 2020, 05:51:27 pm »
Hi,

I wonder if it is possible to setup PRINT USING to display (large) numbers, separated with points instead of comma's.  This is common in EU and possibly other areas.

Wiki http://www.qb64.org/wiki/PRINT_USING says:
  ,.    Comma to left of decimal point, prints a comma every 3 used # digit places left of the decimal point.

This gives me 324,653,221.  if programmed with PRINT USING "##############,."; a  (and I don't need decimals here).
Stripping to PRINT USING "##############,"; a      (undocumented on wiki) makes it even better to  324,653,221

But PRINT USING "##############."; a    doesn't do the trick.

I like to see my number as 324.653.221 with PRINT USING and preferrably without coding that myself in a Function ;-)

Is this possible?


Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: PRINT USING for non US numbers
« Reply #1 on: December 29, 2020, 07:40:37 pm »
I'm not really familiar with PRINT USING templates. I've used them, but they usually devil me terribly. Maybe someone else knows more, but I think it's not generally possible. How would one tell the difference between 1,000,000.321 and 1.000.000.321? Is it a million and three decimal places or a tad over a billion? Sounds like a good way to get something wrong by orders of magnitude. Of course, if it's understood that one is only working with integers then I suppose it would be fine.

In the spirit of addressing the problem I offer the following...

Code: QB64: [Select]
  1.  
  2.     CLS
  3.     INPUT "enter a number for point conversion ", a##
  4.     Point_Number a##
  5.     PRINT "Space to continue"
  6.     SLEEP
  7.  
  8. SUB Point_Number (var AS _FLOAT)
  9.  
  10.     x$ = _TRIM$(STR$(var))
  11.     full% = LEN(x$)
  12.     dec_point% = INSTR(x$, ".")
  13.     IF dec_point% = 0 THEN lngth% = full% ELSE lngth% = dec_point% - 1
  14.     FOR x = 1 TO lngth%
  15.         IF ((lngth% + 1 - x) / 3 = INT((lngth% + 1 - x) / 3)) AND x <> 1 THEN
  16.             y$ = y$ + "."
  17.         END IF
  18.         y$ = y$ + MID$(x$, x, 1)
  19.     NEXT x
  20.     IF dec_point% <> 0 THEN y$ = y$ + MID$(x$, dec_point%)
  21.     PRINT y$
  22.  
  23.  

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: PRINT USING for non US numbers
« Reply #2 on: December 29, 2020, 08:15:10 pm »
Unfortunately PRINT USING is fairly American, and doesn't really know about internationalisation.

Likely you'll have to write a routine yourself (or even better, wait a few days and someone will likely come along with one for you).

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: PRINT USING for non US numbers
« Reply #3 on: January 01, 2021, 03:10:07 pm »
Hi zaadstra
your homework is:
print an integer number using the point for dividing the thousand multiple at the place of the comma.
PRINT USING let format the output of the number but it divides the thousand multiple using the comma.

Here one of the thousand possible solutions
Code: QB64: [Select]
  1.  
  2. i1 = -23200
  3. i2 = 12345678901
  4. PRINT "   Input        Output"
  5. PRINT i1, IntPrintUsing$(STR$(i1))
  6. PRINT i2, IntPrintUsing$(STR$(i2))
  7.  
  8. FUNCTION IntPrintUsing$ (Toprint AS STRING)
  9.     DIM in AS STRING, tmp AS STRING, index AS INTEGER, cont AS INTEGER
  10.     Toprint = _TRIM$(Toprint)
  11.     IF INSTR(Toprint, "-") THEN in = MID$(Toprint, INSTR(Toprint, "-") + 1, LEN(Toprint) - INSTR(Toprint, "-")) ELSE in = Toprint
  12.     cont = 0: index = 0
  13.     WHILE cont < LEN(in)
  14.         IF index + 3 < LEN(in) THEN
  15.             index = index + 3
  16.         ELSE
  17.             index = LEN(in) - index
  18.         END IF
  19.         cont = cont + 3
  20.         IF index > 2 THEN tmp = "." + MID$(in, 1 + LEN(in) - index, 3) + tmp ELSE tmp = MID$(in, 1, index) + tmp
  21.     WEND
  22.     in = tmp
  23.     IF INSTR(Toprint, "-") THEN in = "-" + in
  24.     IntPrintUsing$ = in

this can be optimized to save time (for example storing in a variable the result of LEN(in) instead of calculating each time this same result)

Good Enjoying  with programming
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #4 on: January 01, 2021, 06:42:18 pm »
Unfortunately PRINT USING is fairly American, and doesn't really know about internationalisation.

Likely you'll have to write a routine yourself (or even better, wait a few days and someone will likely come along with one for you).

Ha I was expecting @RhoSigma to come along ;-))  must be on Holidays.



Hi zaadstra
your homework is:
print an integer number using the point for dividing the thousand multiple at the place of the comma.
PRINT USING let format the output of the number but it divides the thousand multiple using the comma.

Here one of the thousand possible solutions
Code: QB64: [Select]
  1.  
  2. i1 = -23200
  3. i2 = 12345678901
  4. PRINT "   Input        Output"
  5. PRINT i1, IntPrintUsing$(STR$(i1))
  6. PRINT i2, IntPrintUsing$(STR$(i2))
  7.  
  8. FUNCTION IntPrintUsing$ (Toprint AS STRING)
  9.     DIM in AS STRING, tmp AS STRING, index AS INTEGER, cont AS INTEGER
  10.     Toprint = _TRIM$(Toprint)
  11.     IF INSTR(Toprint, "-") THEN in = MID$(Toprint, INSTR(Toprint, "-") + 1, LEN(Toprint) - INSTR(Toprint, "-")) ELSE in = Toprint
  12.     cont = 0: index = 0
  13.     WHILE cont < LEN(in)
  14.         IF index + 3 < LEN(in) THEN
  15.             index = index + 3
  16.         ELSE
  17.             index = LEN(in) - index
  18.         END IF
  19.         cont = cont + 3
  20.         IF index > 2 THEN tmp = "." + MID$(in, 1 + LEN(in) - index, 3) + tmp ELSE tmp = MID$(in, 1, index) + tmp
  21.     WEND
  22.     in = tmp
  23.     IF INSTR(Toprint, "-") THEN in = "-" + in
  24.     IntPrintUsing$ = in

this can be optimized to save time (for example storing in a variable the result of LEN(in) instead of calculating each time this same result)

Good Enjoying  with programming

@TempodiBasic

Try more numbers for your Function:
Code: QB64: [Select]
  1.  
  2.     PRINT
  3.     INPUT "Enter an Integer to convert > "; test
  4.     PRINT IntPrintUsing$(STR$(test))
  5. LOOP UNTIL test = 0
  6.  
  7. FUNCTION IntPrintUsing$ (Toprint AS STRING)
  8.     DIM in AS STRING, tmp AS STRING, index AS INTEGER, cont AS INTEGER
  9.     Toprint = _TRIM$(Toprint)
  10.     IF INSTR(Toprint, "-") THEN in = MID$(Toprint, INSTR(Toprint, "-") + 1, LEN(Toprint) - INSTR(Toprint, "-")) ELSE in = Toprint
  11.     cont = 0: index = 0
  12.     WHILE cont < LEN(in)
  13.         IF index + 3 < LEN(in) THEN
  14.             index = index + 3
  15.         ELSE
  16.             index = LEN(in) - index
  17.         END IF
  18.         cont = cont + 3
  19.         IF index > 2 THEN tmp = "." + MID$(in, 1 + LEN(in) - index, 3) + tmp ELSE tmp = MID$(in, 1, index) + tmp
  20.     WEND
  21.     in = tmp
  22.     IF INSTR(Toprint, "-") THEN in = "-" + in
  23.     IntPrintUsing$ = in
  24.  
  25.  

I am not liking how 999 and -999 are looking, how about you?
« Last Edit: January 01, 2021, 06:43:39 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: PRINT USING for non US numbers
« Reply #5 on: January 01, 2021, 08:26:58 pm »
Hi Bplus
Thanks to find a bug to that function!
It didn't manage rightly the number with digits multiple of 3!

Now it seems that it works.

Give a try to this fix.
Code: QB64: [Select]
  1.  
  2. i1 = -23200
  3. i2 = 12345678901
  4. i3 = 799444
  5. i4 = -999
  6. i5 = -111222333
  7. PRINT "   Input        Output"
  8. PRINT i1, IntPrintUsing$(STR$(i1))
  9. PRINT i2, IntPrintUsing$(STR$(i2))
  10. PRINT i3, IntPrintUsing$(STR$(i3))
  11. PRINT i4, IntPrintUsing$(STR$(i4))
  12. PRINT i5, IntPrintUsing$(STR$(i5))
  13.  
  14. FUNCTION IntPrintUsing$ (Toprint AS STRING)
  15.     DIM in AS STRING, tmp AS STRING, index AS INTEGER, cont AS INTEGER
  16.     Toprint = _TRIM$(Toprint)
  17.     IF INSTR(Toprint, "-") THEN in = MID$(Toprint, INSTR(Toprint, "-") + 1, LEN(Toprint) - INSTR(Toprint, "-")) ELSE in = Toprint
  18.     cont = 0: index = 0
  19.     WHILE cont < LEN(in)
  20.         IF index + 3 <= LEN(in) THEN
  21.             index = index + 3
  22.         ELSE
  23.             index = LEN(in) - index
  24.         END IF
  25.         cont = cont + 3
  26.         IF index > 2 THEN tmp = "." + MID$(in, 1 + LEN(in) - index, 3) + tmp ELSE tmp = MID$(in, 1, index) + tmp
  27.     WEND
  28.     IF LEN(in) - cont = 0 THEN tmp = RIGHT$(tmp, LEN(tmp) - INSTR(tmp, "."))
  29.     in = tmp
  30.     IF INSTR(Toprint, "-") THEN in = "-" + in
  31.     IntPrintUsing$ = in

  [ You are not allowed to view this attachment ]  
Programming isn't difficult, only it's  consuming time and coffee

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: PRINT USING for non US numbers
« Reply #6 on: January 01, 2021, 08:29:20 pm »
Ha I was expecting @RhoSigma to come along ;-))  must be on Holidays.

Funny thing, I was just about releasing a bigger update to my IndexFormat() function before the year's end, when this topic popped up, which made me rethink things before the release. So still give me a day or two to finish things and you'll not only able to flip the comma/dot notation, but also to exchange the $ currency sign. Also a feature request on GitHub asks for the ability to group hex/bin/oct outputs, which can be achieved with the new IndexFormat() function too, so stay tuned....
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: PRINT USING for non US numbers
« Reply #7 on: January 01, 2021, 09:30:35 pm »
Waiting the sooner RhoSigma developments
Here another way to do the same work of the first : just use the point instead of comma for thousands multiple on printing on the screen an integer digit.


Code: QB64: [Select]
  1.  
  2. i1 = -23200
  3. i2 = 12345678901
  4. i3 = 799444
  5. i4 = -999
  6. i5 = -11122233
  7. i6 = -111222333
  8. PRINT "   Input        Output   "
  9. PRINT i1, IntPrintUsing$(STR$(i1))
  10. PRINT i2, IntPrintUsing$(STR$(i2))
  11. PRINT i3, IntPrintUsing$(STR$(i3))
  12. PRINT i4, IntPrintUsing$(STR$(i4))
  13. PRINT i5, IntPrintUsing$(STR$(i5))
  14. PRINT i6, IntPrintUsing$(STR$(i6))
  15. PRINT "----Input--------Output---"
  16. PRINT i1, IntPrintUsing2$(STR$(i1))
  17. PRINT i2, IntPrintUsing2$(STR$(i2))
  18. PRINT i3, IntPrintUsing2$(STR$(i3))
  19. PRINT i4, IntPrintUsing2$(STR$(i4))
  20. PRINT i5, IntPrintUsing2$(STR$(i5))
  21. PRINT i6, IntPrintUsing2$(STR$(i6))
  22.  
  23. FUNCTION IntPrintUsing$ (Toprint AS STRING)
  24.     DIM in AS STRING, tmp AS STRING, index AS INTEGER, cont AS INTEGER
  25.     Toprint = _TRIM$(Toprint)
  26.     IF INSTR(Toprint, "-") THEN in = MID$(Toprint, INSTR(Toprint, "-") + 1, LEN(Toprint) - INSTR(Toprint, "-")) ELSE in = Toprint
  27.     cont = 0: index = 0
  28.     WHILE cont < LEN(in)
  29.         IF index + 3 <= LEN(in) THEN
  30.             index = index + 3
  31.         ELSE
  32.             index = LEN(in) - index
  33.         END IF
  34.         cont = cont + 3
  35.         IF index > 2 THEN tmp = "." + MID$(in, 1 + LEN(in) - index, 3) + tmp ELSE tmp = MID$(in, 1, index) + tmp
  36.     WEND
  37.     IF LEN(in) - cont = 0 THEN tmp = RIGHT$(tmp, LEN(tmp) - INSTR(tmp, "."))
  38.     in = tmp
  39.     IF INSTR(Toprint, "-") THEN in = "-" + in
  40.     IntPrintUsing$ = in
  41.  
  42. FUNCTION IntPrintUsing2$ (Toprint AS STRING)
  43.     DIM l AS INTEGER, in AS STRING, Tmp AS STRING, Start AS INTEGER, cont AS INTEGER
  44.     Toprint = _TRIM$(Toprint)
  45.     IF INSTR(Toprint, "-") THEN in = MID$(Toprint, INSTR(Toprint, "-") + 1, LEN(Toprint) - INSTR(Toprint, "-")) ELSE in = Toprint
  46.     l = LEN(in)
  47.     IF l MOD 3 = 0 THEN
  48.         Start = 3
  49.         cont = 1
  50.     ELSE
  51.         Start = l MOD 3
  52.         cont = 0
  53.     END IF
  54.     Tmp = MID$(in, 1, Start)
  55.  
  56.     WHILE cont < l - Start
  57.         cont = cont + 3
  58.         Tmp = Tmp + "." + MID$(in, cont, 3)
  59.     WEND
  60.     IF INSTR(Toprint, "-") THEN Tmp = "-" + Tmp
  61.     IntPrintUsing2$ = Tmp
Thanks to try
Maybe it is possible to extend the function to Single and Double numbers...
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #8 on: January 01, 2021, 10:04:20 pm »
@TempodiBasic  Ah yes the fix looks good but I like the idea of your 2nd approach, specially for real numbers AKA floats AKA having digits to the right of decimal or is it comma now hahaha!

ie do it the American way with PRINT USING then swap . for , and vice versa but save place of decimal first and do commas to dots then at place of decimal put the comma.
« Last Edit: January 01, 2021, 10:10:01 pm by bplus »

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: PRINT USING for non US numbers
« Reply #9 on: January 02, 2021, 07:06:01 am »
Hi guys,

Thanks for all the thinking and even solutions!  I'd never thought a question like this would generate so many solutions :-)

A I noticed the American bias (also in character sets I have to do quite some tricks),  this normally isn't a problem. I was just hoping that in a 'next' version of QB64 the print using options will be added for the 3 digit groups and the decimal comma,  or only the 3 digit groups (like ,. and . in print using).
Print Using indeed has it's quirks but when it is tamed then it solves quite some formatting stuff for you.

Meanwhile, I had written my own function for this,  and funny enough, again another approach like all pieces of code above are different:
To simulate Print using with it's column formatting, I added a column width with parameter 'printlen' and have the number aligned to the right.

Code: QB64: [Select]
  1. PRINT #99, "Matched  :"; PrintNiceRightAlign$(totalbytes1, 16); PrintNiceRightAlign$(totalbytes2, 16)
  2.  
  3. FUNCTION PrintNiceRightAlign$ (printnu&&, printlen)
  4.    ' Parameters: number to print, total character length to be filled
  5.    x$ = LTRIM$(STR$(printnu&&))
  6.    printnu$ = ""
  7.  
  8.    FOR i = LEN(x$) TO 1 STEP -1
  9.       printnu$ = printnu$ + MID$(x$, LEN(x$) - i + 1, 1)
  10.       IF ((i - 1) MOD 3 = 0) AND i <> 1 THEN printnu$ = printnu$ + "."
  11.    NEXT
  12.  
  13.    PrintNiceRightAlign$ = RIGHT$(SPACE$(printlen) + printnu$, printlen)
  14.  
  15. Matched  :   4.574.562.555   4.574.562.555
  16.  
« Last Edit: January 02, 2021, 09:06:44 am by zaadstra »

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
Re: PRINT USING for non US numbers
« Reply #10 on: January 02, 2021, 12:14:10 pm »
A I noticed the American bias

It's not just in the USA that a comma separator is used. We use it here in the UK too. Just out of interest, what country are you in?
Oh look - a sig file :-)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #11 on: January 02, 2021, 12:42:41 pm »
Code: QB64: [Select]
  1.  
  2.     PRINT
  3.     INPUT "Enter an Integer to convert > "; test
  4.     PRINT PrintNiceRightAlign$(test, 20)
  5. LOOP UNTIL test = 0
  6.  
  7.  
  8. FUNCTION PrintNiceRightAlign$ (printnu&&, printlen)
  9.     ' Parameters: number to print, total character length to be filled
  10.     x$ = LTRIM$(STR$(printnu&&))
  11.     printnu$ = ""
  12.  
  13.     FOR i = LEN(x$) TO 1 STEP -1
  14.         printnu$ = printnu$ + MID$(x$, LEN(x$) - i + 1, 1)
  15.         IF ((i - 1) MOD 3 = 0) AND i <> 1 THEN printnu$ = printnu$ + "."
  16.     NEXT
  17.  
  18.     PrintNiceRightAlign$ = RIGHT$(SPACE$(printlen) + printnu$, printlen)
  19.  
  20.  
  21.  

Tested and I think fails on neg numbers:
  [ You are not allowed to view this attachment ]  

But is true you don't need $ for string constants or other suffix for other constants.
« Last Edit: January 02, 2021, 12:48:35 pm by bplus »

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: PRINT USING for non US numbers
« Reply #12 on: January 02, 2021, 12:52:13 pm »
It's not just in the USA that a comma separator is used. We use it here in the UK too. Just out of interest, what country are you in?
I'm in The Netherlands.  (hi neigbour!)

Now out of curiosity, which regions are using what, I did some reading: https://en.wikipedia.org/wiki/Decimal_separator
We seem to owe our system to the Romans :-)
I'm not sure if there is a relation to the use of the imperial system of weights and measures in a country.

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: PRINT USING for non US numbers
« Reply #13 on: January 02, 2021, 01:01:07 pm »


Tested and I think fails on neg numbers:
  [ You are not allowed to view this attachment ]  

But is true you don't need $ for string constants or other suffix for other constants.

That is very much possible!  But as my byte counter only produce positive numbers, I have no issue with that.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: PRINT USING for non US numbers
« Reply #14 on: January 02, 2021, 02:05:08 pm »
EDIT: oops wrong post
« Last Edit: January 02, 2021, 02:07:17 pm by bplus »