Very Simple PRINT Formatting Function
Posted: Tue Oct 17, 2017 7:04 am
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
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
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