Very Simple PRINT Formatting Function

Post Reply
User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Very Simple PRINT Formatting Function

Post by GeorgeMcGinn »

Below is a very simple function that returns a Scoped (or Global) variable .FMT$ with the proper formatting mask.

Right now it does Integers, US Dollars, Decimals out to two places, and even adding spaces. (I have a program in development where I right adjust the number, but I add the number of spaces needed based on the length of the actual number.

I wrote this to address a specific problem, where I use the LEN of the number as the index into the proper array to get the proper mask. (OPTION BASE 1 must be used).

To use this in your program:
I$="DEC2"
X=1234.56
FORMAT(X,I$)

And the format function returns a variable FMT$="#,###,

Here is a snippet of code where in a program I am developing (extract data from JSON and XML file. This code determines the size of the line number on a report, and calculates how many spaces to pace before the number so they all are right-justified. The routine knows the total number of lines, and using the current line number it puts the proper amount of spaces
L$=LINES!Lx=LEN(L$)!I$="SPACES"
FOR L=1 TO LINES
FILE OUTFile$ READLINE Tempstr$
L$=L!Ly=LEN(L$)!Ln=lx-ly
FORMAT(Ln,I$)
Tempstr$=FMT$&L&": "&Tempstr$
FILE RPTFile$ WRITELINE Tempstr$
NEXT L

There is also another format function written by Henko (and the Dutchman) that instead of returning the Format Mask (eg: FMT$="#,###.## for 1234.56) it returns X$ already formated. (X$ ="1,234.56"). The link to it is: viewtopic.php?f=20&t=1314&hilit=Formatting&start=20

Code: Select all

'--------------------------------------------------------
' FORMAT Statement:
'        Routine that takes the lenght of the number, 
'        uses it as an index to get the right FORMAT 
'        mask. I$ is used to tell the program what kind
'        of number X is, then executes the proper
'        formatting.  
'
DEF FORMAT(X,I$)
'    Xi=INT(X)
    X$=X
    L=LEN(X$)

    IF I$="INT" THEN
       DIM FM$(11)
       FM$(1)  = "#"
       FM$(2)  = "##"
       FM$(3)  = "###"
       FM$(4)  = "#,###"
       FM$(5)  = "##,###"
       FM$(6)  = "###,###"
       FM$(7)  = "#,###,###"
       FM$(8)  = "##,###,###"
       FM$(9)  = "###,###,###"
       FM$(10) = "#,###,###,###"
       FM$(11) = "##,###,###,###"      'This is for ResolveFILE
    ENDIF

    IF I$="USD" THEN
       DIM FM$(10)
       FM$(1)  = "$#.##"
       FM$(2)  = "$##.##"
       FM$(3)  = "$###.##"
       FM$(4)  = "$#,###.##"
       FM$(5)  = "$##,###.##"
       FM$(6)  = "$###,###.##"
       FM$(7)  = "$#,###,###.##"
       FM$(8)  = "$##,###,###.##"
       FM$(9)  = "$###,###,###.##"
       FM$(10) = "$#,###,###,###.##"
    ENDIF
    
    IF I$="DEC2" THEN
       DIM FM$(10)
       FM$(1)  = "#.##"
       FM$(2)  = "##.##"
       FM$(3)  = "###.##"
       FM$(4)  = "#,###.##"
       FM$(5)  = "##,###.##"
       FM$(6)  = "###,###.##"
       FM$(7)  = "#,####,###.##"
       FM$(8)  = "##,###,###.##"
       FM$(9)  = "###,###,###.##"
       FM$(10) = "#,###,###,###.##"
    ENDIF

    IF I$="SPACES" THEN
       DIM FM$(10)
       FM$(1)  = " "
       FM$(2)  = "  "
       FM$(3)  = "   "
       FM$(4)  = "    "
       FM$(5)  = "     "
       FM$(6)  = "      "
       FM$(7)  = "       "
       FM$(8)  = "        "
       FM$(9)  = "         "
       FM$(10) = "          "
       IF X>0 THEN
          .FMT$=FM$(X)
       ELSE
          .FMT$=""
       ENDIF
    ELSE
       .FMT$=FM$(L)
    ENDIF

ENDDEF
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Very Simple PRINT Formatting Function

Post by rbytes »

Hi, George. I just noticed this function, and I thought it would be useful to me. I copied it to my iPad and inserted the three lines above that you suggested testing it with.

It terminates with an error. Would you please take a look? I have included a screen shot of the error.
Here is the code as I believe you suggested it be set up for testing.

Code: Select all

'--------------------------------------------------------
' FORMAT Statement:
'        Routine that takes the lenght of the number, 
'        uses it as an index to get the right FORMAT 
'        mask. I$ is used to tell the program what kind
'        of number X is, then executes the proper
'        formatting.  
'

I$="DEC2"
X=1234.56
FORMAT(X,I$)

DEF FORMAT(X,I$)
'    Xi=INT(X)
    X$=X
    L=LEN(X$)

    IF I$="INT" THEN
       DIM FM$(11)
       FM$(1)  = "#"
       FM$(2)  = "##"
       FM$(3)  = "###"
       FM$(4)  = "#,###"
       FM$(5)  = "##,###"
       FM$(6)  = "###,###"
       FM$(7)  = "#,###,###"
       FM$(8)  = "##,###,###"
       FM$(9)  = "###,###,###"
       FM$(10) = "#,###,###,###"
       FM$(11) = "##,###,###,###"      'This is for ResolveFILE
    ENDIF

    IF I$="USD" THEN
       DIM FM$(10)
       FM$(1)  = "$#.##"
       FM$(2)  = "$##.##"
       FM$(3)  = "$###.##"
       FM$(4)  = "$#,###.##"
       FM$(5)  = "$##,###.##"
       FM$(6)  = "$###,###.##"
       FM$(7)  = "$#,###,###.##"
       FM$(8)  = "$##,###,###.##"
       FM$(9)  = "$###,###,###.##"
       FM$(10) = "$#,###,###,###.##"
    ENDIF
    
    IF I$="DEC2" THEN
       DIM FM$(10)
       FM$(1)  = "#.##"
       FM$(2)  = "##.##"
       FM$(3)  = "###.##"
       FM$(4)  = "#,###.##"
       FM$(5)  = "##,###.##"
       FM$(6)  = "###,###.##"
       FM$(7)  = "#,####,###.##"
       FM$(8)  = "##,###,###.##"
       FM$(9)  = "###,###,###.##"
       FM$(10) = "#,###,###,###.##"
    ENDIF

    IF I$="SPACES" THEN
       DIM FM$(10)
       FM$(1)  = " "
       FM$(2)  = "  "
       FM$(3)  = "   "
       FM$(4)  = "    "
       FM$(5)  = "     "
       FM$(6)  = "      "
       FM$(7)  = "       "
       FM$(8)  = "        "
       FM$(9)  = "         "
       FM$(10) = "          "
       IF X>0 THEN
          .FMT$=FM$(X)
       ELSE
          .FMT$=""
       ENDIF
    ELSE
       .FMT$=FM$(L)
    ENDIF

ENDDEF
Attachments
4C1D053B-BCCC-4EEA-A1E8-A18757BB2606.png
4C1D053B-BCCC-4EEA-A1E8-A18757BB2606.png (3.34 MiB) Viewed 4981 times
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Very Simple PRINT Formatting Function

Post by Dutchman »

OPTION BASE 1 is missing ;)

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Very Simple PRINT Formatting Function

Post by GeorgeMcGinn »

Yes it is. I should have noted in the post that this works with OPTION BASE 1. So if you work with OPTION BASE 0, either change the function to fit the way you program or add it to the beginning of the fuction, then change it back before doing the return.

Dutchman wrote:
Mon Nov 06, 2017 10:52 am
OPTION BASE 1 is missing ;)
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Very Simple PRINT Formatting Function

Post by rbytes »

Thanks to both of you. I got it working last night by just increasing the DIMs by 1 each, since I needed it to work with OPTION BASE 0.

George, I would still suggest adding OPTION BASE 1 to your example so that users can get it to work without any correction needed. To actually see it format a number, they would need to include your three example lines and then the line PRINT X.

You could just increase the DIMs by 1 each, and then your code works with OPTION BASE 0, OPTION BASE 1, or no OPTION BASE statement (defaults to 0)
The only thing that gets me down is gravity...

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Very Simple PRINT Formatting Function

Post by GeorgeMcGinn »

If I was submitting a normal program, it would have all the OPTION BASE including any changes from 1 to 0 or 0 to 1 and back again.

Since this category is Libraries, all we have to produce is the library. Yes, in my write up I should say what OPTION BASE it is coded for, but I would suspect most programmers realize that this is just to provide the LIB, partial code, and are smart enough to make the changes so it works for them.

Am I reaching too high as far as how knowledgeable programmers are here?

I agree, that it should be in the write up and I will provide that in the future. But I can find many LIBS provided that has the same issue mine has. It's just that since it uses the length of the number as the index into the array, if you use OPTION BASE 0, you can 1) Add one to the index; 2) put the code Henko uses to save the prior OPTION BASE, put in the OPTION BASE 1 and restore the 9PTIOM BASE before leaving the LIB, as the .FMT$ will have the correct format mask.

Don't forget, this is just a quick and dirty way to add formatting to your PRINT statement.

Dutchman and Henko wrote a series of Functions that actually returns the number already formatted. That should eventually also be placed here, as its use can appear in math (if I recall correctly) or used when printing multiple formatted numbers, and can be used directly into the PRINT statement, or building a STRING variable and concatenating it directly into the STRING.

George.
rbytes wrote:
Tue Nov 07, 2017 4:02 am
Thanks to both of you. I got it working last night by just increasing the DIMs by 1 each, since I needed it to work with OPTION BASE 0.

George, I would still suggest adding OPTION BASE 1 to your example so that users can get it to work without any correction needed. To actually see it format a number, they would need to include your three example lines and then the line PRINT X.

You could just increase the DIMs by 1 each, and then your code works with OPTION BASE 0, OPTION BASE 1, or no OPTION BASE statement (defaults to 0)
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Very Simple PRINT Formatting Function

Post by rbytes »

When I asked Mr. K to create this section, I wanted it to contain working examples. Users could simply copy the code, run it, and see the results. The idea was to save us all time and effort in having to recreate the same functions.

Most of the contributions do work exactly that way. If elements are missing and users have to debug the code, that makes the contributions less useful. I would like to see the code examples work out of the box rather than have users waste their time figuring out why they don’t work.
The only thing that gets me down is gravity...

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Very Simple PRINT Formatting Function

Post by GeorgeMcGinn »

You are right, and with so much going on, I forgot that.

At the next chance I get, I will actually post the full function that has the code to test it.

What you had asked for was the formatting code used in the Tree.sb, and in my haste, I just cut/pasted the code right out of that program, which actually is not the whole function as I split out the DIM statements and some others so it would work in the structure that Ton coded.

By Friday I will post the format function as a new topic, and it will have the proper code to show it works.

I know in my original post I provided the three lines to make it work, and I showed the more complex code that it came from the JSON/XML System that I am currently working on. I just never packed it neatly.

I will correct that, but this post only addresses the question of what I did to Tree.sb, and actually split out parts of the full function as it would not have worked right.

Thanks to you and Dutchman for pointing all this out.

I'm working on some major code (hoping SmartBASIC will stay around) and, also by Friday, check out my post there, as I am testing something, that if it works, we can possibly still use Dropbox even if Mr. K has to pull it. Before reading this and working on the File Compare (finishing up the Help File panels) one of the next changes to FC is to allow you to compare a file via URL to one on disk, or compare two files using URLs, and I had a Eureka moment for code I had already written and posted, but I need to test it first before I show how it can work and be the start of a work-a-round.
rbytes wrote:
Tue Nov 07, 2017 3:39 pm
When I asked Mr. K to create this section, I wanted it to contain working examples. Users could simply copy the code, run it, and see the results. The idea was to save us all time and effort in having to recreate the same functions.

Most of the contributions do work exactly that way. If elements are missing and users have to debug the code, that makes the contributions less useful. I would like to see the code examples work out of the box rather than have users waste their time figuring out why they don’t work.
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Post Reply