Format numbers and strings in a stringfield - procedure

Share your advanced PureBasic knowledge/code with the community.
Rudy M
User
User
Posts: 44
Joined: Fri Aug 23, 2024 1:18 pm

Format numbers and strings in a stringfield - procedure

Post by Rudy M »

Hello,
As a new PB user I have a large number of old basic programs of which I convert several to Purebasic.

I caught myself that I needed a lot of time again and again to convert the old basic syntax with tab(x), printusing "#####...." etc... to Purebasic.
Especially a multi-dim. array to print properly on the screen.
The same with geographical coordinates + info about those places in Comma-separated values ​​("CSV" files).
Greetings,
Rudy M

Code: Select all

EnableExplicit

;Variables for Format_in_Field
Define Input_Str$,StrLeft.l, Input_Num.f, Decimals.l, DecimalPoint$, ThousandSeparator$, FieldLength.l, BeginChrs$, EndChrs$, Fill_Chr$
Define Getal.d
Define Getal2.f
Define Getal3.l

;Variabels with content for Format_in_Field procedure:
Input_Str$ = ""
StrLeft = #False
Decimals = 2
DecimalPoint$ = "."
ThousandSeparator$ = "."
FieldLength = 20
Fill_Chr$ = " "
BeginChrs$ = ""
EndChrs$ = " Euro"     ;Can also be use for p.e.:  °C , °F, Euro's, Dollars, kg, g, l, pcs...

;Remark:  Dutch 'Getal' = Englisch 'Number'
Getal = 1250.125    ;double      
Getal2 = 1250.125   ;float
Getal3 = 1250       ;long integer


Declare.s Format_in_Field (Input_Str$, StrLeft.l, Input_Num.f, Decimals.l, DecimalPoint$, ThousandSeparator$, FieldLength.l, BeginChrs$, EndChrs$, Fill_Chr$)
;INFO:
;If Input_Str$ is empty then Input_Num takes precedence
;StrLeft     = #True then place left, els place right in field
;Input_Num   = number but... ignored when Input_Str$ is not empty (consider 'spaces' also as 'non empty')
;Decimals    = the number of decimals after the decimal point
;ThousandSeparator$  So You can take a point '.' or a comma ',' as separator
;FieldLength = total lenght of the field (to display in screen).
;BeginChrs$  = Can be a textstring, but You could place here a '|' to clearly indicate a column.
;EndChrs$    = idem
;FillChrs$   = spaces or  according everything to your choice (P.e: . * /  #  ! x _ )

Procedure.s Format_in_Field (Input_Str$, StrLeft.l, Input_Num.f, Decimals.l, DecimalPoint$, ThousandSeparator$, FieldLength.l, BeginChrs$, EndChrs$, Fill_Chr$)
  ;If Input_Str$ is empty then Input_Num takes precedence
  
  Protected txt$, BeginChrLenght.l, EndChrLenght.l, NewFieldLength.l
  
  BeginChrLenght = Len (BeginChrs$)
  EndChrLenght   = Len (EndChrs$)
  
  If Input_Str$ = ""
    txt$ = FormatNumber(Input_Num, Decimals, DecimalPoint$, ThousandSeparator$)
  Else
    txt$ = Input_Str$
  EndIf   
  
  NewFieldLength = FieldLength - BeginChrLenght - EndChrLenght  
  
  If Input_Str$ = "" ;Input_Str$ is empty
    ;its a number then place right                   (You could change the listing to place numbers also left...)
    txt$ = RSet(txt$ , NewFieldLength, Fill_Chr$)
  Else
    ;its a string, so two possibitys (left of right...)
    If StrLeft = #True  ; place left
      txt$ = LSet(txt$ , NewFieldLength, Fill_Chr$)
    Else                ;place right
      txt$ = RSet(txt$ , NewFieldLength, Fill_Chr$)
    EndIf   
  EndIf
  
  txt$ = BeginChrs$ + txt$
  ProcedureReturn  BeginChrs$ + txt$ + EndChrs$
  
EndProcedure


;DEMO 
;Remark: Only to show a place ">" or "<" in debug outside my procedure.

;Result$ = FormatNumber(Number.d [, NbDecimals [, DecimalPoint$ [, ThousandSeparator$]]])
Debug "Only with 'FormatNumber:"
Debug "======================="
Debug "double precision, test out rounding for your purpose!!!:"
Debug FormatNumber(Getal     , Decimals   , DecimalPoint$   , ThousandSeparator$)  
Debug "float precision , test out rounding for your purpose!!!:"
Debug FormatNumber(Getal2    , Decimals   , DecimalPoint$   , ThousandSeparator$)  
Debug "long int:"
Debug FormatNumber(Getal3    , Decimals   , DecimalPoint$   , ThousandSeparator$)  

Debug "": Debug "": Debug " "
Debug "And as surplus with 'RSet;"
Debug "=========================="
;Result$ = RSet(String$, Length [, Fill_Chr$])
Debug "Total fieldlenght = 20 for use with PB 'RSET':"
Debug ">" + RSet(FormatNumber(Getal     , Decimals   , DecimalPoint$   , ThousandSeparator$), FieldLength, Fill_Chr$) + "<"
Debug ">" + RSet(FormatNumber(Getal2     , Decimals   , DecimalPoint$   , ThousandSeparator$), FieldLength, Fill_Chr$) + "<" 
Debug ">" + RSet(FormatNumber(Getal3     , Decimals   , DecimalPoint$   , ThousandSeparator$), FieldLength, Fill_Chr$) + "<"

Debug "": Debug "": Debug " "
Debug "Via procedure plus begintext and/or endtext or sign:"
Debug "============================================================"
Debug "With '>'  and  '<' at begin of end of the string:"
Debug ">" + Format_in_Field (Input_Str$, StrLeft.l, Getal , Decimals.l , DecimalPoint$  , ThousandSeparator$, FieldLength.l, BeginChrs$, EndChrs$, Fill_Chr$) + "<"

Debug "": Debug "": Debug " "
Debug "Text Right in field Without both > and < signs:"
Debug "============================================================"
Debug ">" + RSet(FormatNumber(Getal3     , Decimals   , DecimalPoint$   , ThousandSeparator$), FieldLength, Fill_Chr$) + "<"
Input_Str$ = "Rudy": EndChrs$=""
Debug ">" + Format_in_Field (Input_Str$, StrLeft.l, Getal , Decimals.l , DecimalPoint$  , ThousandSeparator$, FieldLength.l, BeginChrs$, EndChrs$, Fill_Chr$) + "<"
Debug "Text Left in field Without both > and < signs:"
Input_Str$ = "Rudy" : StrLeft = #True
Debug ">" +Format_in_Field (Input_Str$, StrLeft.l, Getal , Decimals.l , DecimalPoint$  , ThousandSeparator$, FieldLength.l, BeginChrs$, EndChrs$, Fill_Chr$) + "<"
Debug "============================================================"
Debug " test string longer then FieldLength" 
Input_Str$ = "Rudy 123456789012345678901234567890" : 
Debug ">" +Format_in_Field (Input_Str$, StrLeft.l, Getal , Decimals.l , DecimalPoint$  , ThousandSeparator$, FieldLength.l, BeginChrs$, EndChrs$, Fill_Chr$) + "<" 
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Format numbers and strings in a stringfield - procedure

Post by idle »

Thanks for sharing.
Post Reply