DECLARE FUNCTIONI USING$(...) AS STRING
$TYPECHECK ON
$ESCAPECHARS ON
CONST SGNNO%=0:CONST SGNFRONT%=1:CONST SGNBACK%=2
CONST symbols$="#.,-+^*$&! \\"
CONST signs$="- +"
CONST log10=2.302585
CONST thou$="."
CONST dec$=","
CONST curr$="$"
CONST fill$="*"
FUNCTIONI USING$(...)
DIM FORMAT$ AS STRING,OUTPUT$ AS STRING ,a$ AS STRING
DIM ipart$ AS STRING, fpart$ AS STRING,epart$ AS STRING,spart$ AS STRING
DIM valcount% AS INTEGER,strcount% AS INTEGER, innum% AS INTEGER,THOU% AS INTEGER,expo% AS INTEGER
DIM DECPOS% AS INTEGER,SIGN% AS INTEGER,PAD% AS INTEGER,currency% AS INTEGER
DIM SKIP% AS INTEGER,oom% AS INTEGER,decplaces% AS INTEGER,totplaces% AS INTEGER
DIM i% AS INTEGER, xx% AS INTEGER,num# AS DOUBLE,b# AS DOUBLE
DIM ptr% AS INTEGER,f$ AS STRING,i$ AS STRING,places% AS INTEGER
DIM pad$ AS STRING,j% AS INTEGER,k% AS INTEGER,neg% AS INTEGER
DIM INSTRING% AS INTEGER,backslash% AS INTEGER
output$=""
valcount%=1:strcount%=1
IF PARAMSTRCOUNT THEN
format$=PARAMSTR$(1)
INC strcount%
output$=""
ptr%=1
GOSUB reset
WHILE ptr%=<LEN(format$)
a$=MID$(format$,ptr%,1)
xx%=INSTR(symbols$,a$)
IF xx% AND (skip%=0) THEN
SELECT CASE xx%
CASE 1
INC innum%
CASE 2
INC innum%: DECPOS%=innum%
CASE 3
IF innum% AND (decpos%=0)THEN :INC innum%:thou%=1: ELSE: GOSUB buildoutput:END IF
CASE 4
IF innum% THEN INC innum%:sign%=sgnback% ELSE skip%=1
CASE 5
INC innum%: sign%=sgnfront%
CASE 7
INC pad%
IF pad%=2 THEN INC innum%,2
CASE 6
INC expo%
CASE 8
INC currency%
IF currency%=2 THEN INC innum%,2
CASE 9
instring%=-1
CASE 10
INC instring%
CASE 12
INC instring%:INC backslash%
CASE 11
IF backslash%=1 THEN :INC instring%,1 :ELSE :GOSUB buildoutput :END IF
END SELECT
ELSE
GOSUB buildoutput
END IF
INC ptr%
WEND
a$="":GOSUB buildoutput
END IF
FOR i%=valcount% TO PARAMVALCOUNT:output$=output$+" "+ STR$(PARAMVAL(i%)):NEXT
FOR i%=strcount% TO PARAMSTRCOUNT:output$=output$+" "+PARAMSTR$(i%):NEXT
using$=output$
EXIT FUNCTIONI
buildoutput:
IF instring% THEN
IF strcount%<=PARAMSTRCOUNT THEN
IF instring%=-1 THEN
ipart$=PARAMSTR$(strcount%)
ELSE
ipart$=LEFT$(PARAMSTR$(strcount%)+SPACE$(instring%),instring%)
END IF
output$=output$+ipart$
INC strcount%
END IF
ELSEIF innum% THEN
IF sign%=innum% THEN
output$=output$+"+"
ELSEIF decpos%=1 AND innum%=1 THEN
output$=output$+"."
ELSE
IF valcount%=<PARAMVALCOUNT THEN
num#=(PARAMVAL(valcount%))
IF num# THEN oom%=ROUND(LOG(ABS(num#))/log10)+1 ELSE oom%=0
fpart$="":epart$="":spart$=""
IF sign%=1 THEN
spart$=MID$(signs$,SGN(num#)+2,1)
DEC innum%:DEC decpos%:IF decpos%<0 THEN decpos%=0
ELSE
neg%=1
END IF
num#=ABS(num#)
totplaces%=IIF(decpos%,decpos%-1,innum%)
places%=totplaces%-(thou%*((totplaces%-1)\ 3) )-(currency%>0)
decplaces%=IIF (decpos%,innum%-decpos%,0)
IF expo%=4 THEN
j%=oom%-places%
IF num# THEN k%=oom%-places% ELSE k%=0
IF j%>0 THEN DEC j%:INC K%
epart$="E"+MID$(SIGNS$,SGN(k%)+2,1)+RIGHT$("00"+LTRIM$(STR$(ABS(k%))),2)
num#=num#/(10^j%)
END IF
b#=10^decplaces%
num#=FIX(num#*b#+0.5*SGN(num#))/b#
i$=LTRIM$(STR$(FIX(num#)))
IF LEN(i$)>places% THEN
spart$="":epart$=""
ipart$="%"+STR$(PARAMVAL(valcount%))
ELSE
pad$=IIF(pad%=2,fill$," ")
ipart$=STRING$(totplaces%,pad$)
j%=IIF (decpos%,decpos%-1,innum%):k%=LEN(i$)
FOR i% =1 TO LEN(i$)
IF thou% THEN IF i%>1 AND ((i% MOD 3) =1) THEN ipart$= REPLACE$(ipart$,thou$,j%):DEC j%
ipart$=REPLACE$(ipart$,MID$(i$,k%,1),j%)
DEC j%:DEC k%
NEXT
IF currency% THEN ipart$=REPLACE$(ipart$,curr$,j%):DEC j%
IF sign%=0 AND neg%=1 THEN ipart$=REPLACE$(ipart$,"-",j%)
IF decplaces% THEN
b#=10^decplaces%
num#=FIX(FRAC(ABS(num#))*b#+0.5*SGN(num#))/b#
F$=MID$(STR$(num#),3)
fpart$=dec$+LEFT$(f$+STRING$(decplaces%,"0"),decplaces%)
END IF
END IF
output$=output$+spart$+ipart$+fpart$+epart$
INC valcount%
END IF
END IF
ELSEIF pad%=1 THEN
output$=output$+"*"
ELSEIF currency%=1 THEN
output$=output$+"$"
ELSEIF expo% THEN
output$=output$+STRING$(expo%,"^")
END IF
output$=output$+a$
reset:
a$=""
backslash%=0:pad%=0
innum%=0
DECPOS%=0:THOU%=0:SIGN%=0:PAD%=0:currency%=0:expo%=0
instring%=0
skip%=0
RETURN
END FUNCTIONI
$ESCAPECHARS ON
PRINT using$("! can tell you This is a & Test&","Idiot","silly",", Is'nt?")
PRINT USING$("This -\a is a fake format-!",111,"hello")
DIM frm$ AS STRING
frm$= "+## \\ \\ , FOB, cost $$,####.## each. +#,###.#^^^^ "
PRINT using$(frm$,2,"apples",0.2,-1)
PRINT using$(frm$,34,"cars",1010,345678907)
PRINT using$(frm$,0,"cars",0,0)
PRINT using$(frm$,-3,"Computers",-345.6,0.000023)
?"Press any key..":DO:LOOP UNTIL LEN(INKEY$)
|
|