|
Post by Walt Decker on Feb 21, 2022 16:11:34 GMT -5
|
|
rnbw
New Member
Posts: 48
|
Post by rnbw on Feb 21, 2022 18:01:37 GMT -5
Thank you for the link. I'll have a look at the code.
|
|
|
Post by Walt Decker on Oct 4, 2022 10:39:02 GMT -5
A few more useful functions: ' STRUCT tDbl, _ Dbl AS DOUBLE
Fmt$ = "" Value = 1011.101 Fmt$ = FN.Format$(STR$(Value), 4, 1) PRINT USING(Fmt$, Value)
Fmt$ = FN.Format$(STR$(Value), 4, 0) PRINT USING(Fmt$, Value)
tDbl.Dbl.struct = 4 * ATN(1) / 180.0 Fmt$ = FN.Format$(STR$(tDbl.Dbl.struct), -1, 0)
PRINT USING(Fmt$, tDbl.Dbl.struct) Fmt$ = FN.Pad$(STR$(Value), "$", 10, 1) PRINT Fmt$
Fmt$ = FN.Pad$("Peter", " ", 10, 0) PRINT Fmt$
tDbl.Dbl.struct = 1234567890.1119 Fmt$ = FN.Hundred$(tDbl.Dbl.struct, 4) PRINT Fmt$
wait
'--------------------------------------- '---------------------------------------
FUNCTION FN.Format$(StrVal$, NumDecimals, Integer) '####################################################### ' Creates a number template based on the number of ' decimal digits desired and whether a real number ' is to be displayed as an integer or a real. ' ' ARGUMENTS: ' StrVal$: String representation of a number 'NumDecimals: Number of decimals desired. If this ' Number is -1 the function will determine ' the number of decimal digites in the ' number. ' Integer: If this is > 0 the number template is created ' for an integer regardless of the number of ' decimal digets specified. '#######################################################
DotPos = 0 I = 0
ValStr$ = "" LftMsk$ = "" RgtMsk$ = "" Lft$ = "" Rgt$ = ""
FntMsk$ = "#" Dot$ = "."
ValStr$ = StrVal$
IF NumDecimals < 1 THEN DotPos = INSTR(ValStr$, Dot$) IF DotPos = 0 THEN ValStr$ = FN.RepeatStr$(FntMsk$, LEN(ValStr$))
IF Integer THEN FN.Format$ = ValStr$ EXIT FUNCTION END IF
FN.Format$ = ValStr$ + Dot$ + FntMsk$ EXIT FUNCTION END IF
Lft$ = LEFT$(ValStr$, DotPos - 1) Rgt$ = MID$(ValStr$, DotPos + 1) LftMsk$ = FN.RepeatStr$(FntMsk$, LEN(Lft$)) RgtMsk$ = FN.RepeatStr$(FntMsk$, LEN(Rgt$))
IF Integer THEN FN.Format$ = LftMsk$ EXIT FUNCTION END IF
FN.Format$ = LftMsk$ + Dot$ + RgtMsk$ EXIT FUNCTION END IF
RgtMsk$ = FN.RepeatStr$(FntMsk$, NumDecimals)
DotPos = INSTR(ValStr$, Dot$) IF DotPos = 0 THEN Lft$ = FN.RepeatStr$(FntMsk$, LEN(ValStr$))
IF Integer THEN FN.Format$ = Lft$ EXIT FUNCTION END IF
ValStr$ = Lft$ + Dot$ + RgtMsk$ FN.Format$ = ValStr$ EXIT FUNCTION END IF
Lft$ = LEFT$(ValStr$, DotPos - 1) Lft$ = FN.RepeatStr$(FntMsk$, LEN(Lft$))
IF Integer THEN FN.Format$ = Lft$ EXIT FUNCTION END IF
ValStr$ = Lft$ + Dot$ + RgtMsk$ FN.Format$ = ValStr$ END FUNCTION
'--------------------------------------- '---------------------------------------
FUNCTION FN.RepeatStr$(RepStr$, NumChrs) '####################################################### ' Creates a string of characters. ' ' ARGUMENTS: ' RepStr$: String to create ' NumChrs: Number of times the "RepStr$" is duplicated '#######################################################
I = 0
StrOut$ = ""
FOR I = 1 TO NumChrs StrOut$ = StrOut$ + RepStr$ NEXT I
FN.RepeatStr$ = StrOut$ END FUNCTION
'--------------------------------------- '---------------------------------------
FUNCTION FN.Pad$(PadStr$, PadChr$, NumChrs, Back) '####################################################### ' Pads the front or back of a string. Used primarily ' to create strings of the same length. ' ' ARGUMENTS: ' PadStr$: String to pad ' PadChr$: Character(s) with which to create the padding ' NumChrs: Desired length of the final string ' Back: If this value is > 0 the padding will be placed ' at the end of the string to pad. '#######################################################
Padding = 0 PadLen = LEN(PadStr$)
StrOut$ = "" Padded$ = ""
Padding = NumChrs - PadLen IF Padding < 1 THEN FN.Pad$ = PadStr$ EXIT FUNCTION END IF
Padded$ = FN.RepeatStr$(PadChr$, Padding)
IF Back THEN StrOut$ = PadStr$ + Padded$ FN.Pad$ = StrOut$ EXIT FUNCTION END IF
StrOut$ = Padded$ + PadStr$ FN.Pad$ = StrOut$ END FUNCTION
'--------------------------------------- '---------------------------------------
FUNCTION FN.InsertStr$(MainStr$, StrIn$, Posn) '####################################################### ' Inserts a string ' ' ARGUMENTS: ' MainStr$: String in which to insert characters ' StrIn$: String to insert ' Posn: Character position at which to insert ' the string '#######################################################
Lft$ = LEFT$(MainStr$, Posn) Rgt$ = MID$(MainStr$, Posn + 1)
MainStr$ = Lft$ + StrIn$ + Rgt$
FN.InsertStr$ = MainStr$ END FUNCTION
'--------------------------------------- '---------------------------------------
FUNCTION FN.Hundred$(Value, NumDecimals) ' Creates a number string with hundreds seperated by commas ' ' ARGUMENTS: ' Value: Number to seperate into groups of 100 'NumDecimals: Number of decimal digets desired ' ' NOTE: Due to the nature of binary numbers, some real ' numbers can not be correctly expressed. Therefore the ' programmer should provide a value for "NumDecimals" that ' will address the most significant digites of the decimal ' part of the number. '#######################################################
DotPos = 0 Num100 = 0 LftLen = 0 Posn = 0
Lft$ = "" Rgt$ = "" Dot$ = "."
Hundred$ = STR$(Value) IF INSTR(UPPER$(Hundred$), "E") THEN Lft$ = FN.Format$(STR$(Value), -1, 0) Hundred$ = USING(Lft$, Value) Hundred$ = TRIM$(Hundred$) IF LEFT$(Hundred$, 1) = "%" THEN Hundred$ = MID$(Hundred$, 2) END IF END IF
DotPos = INSTR(Hundred$, Dot$) IF DotPos THEN Lft$ = LEFT$(Hundred$, DotPos - 1) Rgt$ = MID$(Hundred$, DotPos) IF NumDecimals > -1 THEN IF NumDecimals = 0 THEN Rgt$ = "" ELSE Rgt$ = LEFT$(Rgt$, NumDecimals + 1) END IF END IF ELSE Lft$ = Hundred$ END IF
LftLen = LEN(Lft$) Num100 = LftLen / 3 IF LftLen MOD(3) = 0 THEN Num100 = Num100 - 1 Posn = LftLen - 3
FOR I = 1 TO Num100 Lft$ = FN.InsertStr$(Lft$, ",", Posn) Posn = Posn - 3 NEXT I
Hundred$ = Lft$ + Rgt$ FN.Hundred$ = Hundred$ END FUNCTION '
|
|