Post by Walt Decker on Mar 11, 2022 14:57:23 GMT -5
Without invoking Liberty Basic's USING() function, huge values of the integer number subset can be displayed. However, only up to 8 digits can be displayed when the decimal number set is used. As the number of integer digits increase the number of decimal digits decrease. At eight integer digits, only one decimal digit can be displayed. At nine integer digits, Liberty Basic converts the decimal number to scientific notation. At this point one can use the USING() function to accurately display the number with an accuracy of up to six decimal digits. Beyond six decimal digits the remaining digits are not accurate. This may be a quirk of the computer binary number system.
The following code displays this and uses the Liberty Basic USING() function to format the numbers so that they may be displayed with decimal points aligned and the hundreds fields separated with a comma.
The following code displays this and uses the Liberty Basic USING() function to format the numbers so that they may be displayed with decimal points aligned and the hundreds fields separated with a comma.
'
DIM Numbers(6)
Numbers(0) = 10919635748.69798564
Numbers(1) = -10653785
Numbers(2) = 1976.42
Numbers(3) = -19074252.83882
Numbers(4) = 1.96375893184
Numbers(5) = -100
Numbers(6) = 5107196357482
MaxLen = -999
FOR I = 0 TO 6
NumStr$ = STR$(Numbers(I))
print NumStr$
IF INSTR(UPPER$(NumStr$), "E") THEN '<--- number as scientific notation
NumStr$ = USING("####################.#########", Numbers(I))'<--- translate
'to a string
NumStr$ = TRIM$(NumStr$) '<--- remove leading and trailing white space
END IF
'<--------- extract the integer part -------->
Dot = INSTR(NumStr$, ".")
IF Dot = 0 THEN
Lft$ = NumStr$
ELSE
Lft$ = LEFT$(NumStr$, Dot - 1)
END IF
IF LEFT$(Lft$, 1) = "-" THEN '<--- remove negative sign
Lft$ = MID$(Lft$, 2)
END IF
MaxLen = MAX(MaxLen, LEN(Lft$)) '<--- find the maximum lenght of the numeric
'group
NumGroups = MAX(NumGroups, INT(LEN(Lft$) / 3)) '<--- find maximum number of
'100s groups
NEXT I
MaxLen = MaxLen + NumGroups + 1 '<--- add max length and max 100s groups
'together plus one for possible negativ
'number
FOR I = 0 TO 6
NumStr$ = FN.Fmt$(Numbers(I), ",", " ", MaxLen) '<--- format the numbers so
'that decimals will align.
'The integer number set has
'an implied decimal value of
'zero
PRINT NumStr$
NEXT I
END
'------------------------------------------------------
'------------------------------------------------------
FUNCTION FN.REPEAT$(StrIn$, Ntimes)
'############################################
' CREATE A STRING Ntimes
'############################################
I = 0
StrOut$ = ""
FOR I = 1 TO Ntimes
StrOut$ = StrOut$ + StrIn$
NEXT I
FN.REPEAT$ = StrOut$
END FUNCTION
'----------------------------------------------------
'----------------------------------------------------
FUNCTION FN.Fmt$(Number, MidDlm$, PadStr$, MaxLen)
NumStr$ = "" '<--- translate "Number" to a string
IntStr$ = "" '<--- integer part of number
DecStr$ = "" '<--- decimal part of number
Minus$ = "-" '<--- negative number indicator
I = 0 '<--- counter
Dot = 0 '<--- decimal point position
Hundreds = 0 '<--- # of 100's
Remain = 0 '<--- part of integer that is not 100
Padding = 0 '<--- how much to add to string
NumStr$ = STR$(Number) '<--- translate number to string
IF INSTR(UPPER$(NumStr$), "E") THEN '<--- check for scientific notation
NumStr$ = USING("####################.#########", Number) '<--- reformat
NumStr$ = TRIM$(NumStr$) '<--- remove white space
END IF
Dot = INSTR(NumStr$, ".") '<--- find decimal point
''<------- extract integer and decimal parts ----->
IF Dot = 0 THEN
IntStr$ = NumStr$
ELSE
IntStr$ = LEFT$(NumStr$, Dot - 1)
DecStr$ = MID$(NumStr$, Dot)
END IF
IF LEFT$(IntStr$, 1) = Minus$ THEN '<--- is number negative
IntStr$ = MID$(IntStr$, 2) '<--- remove negative sign
Padding = MaxLen - LEN(IntStr$) '<how much padding is needed
'<--- determine type of padding to use ------->
IF PadStr$ = "0" THEN
Padding = Padding - 3
IntStr$ = (FN.REPEAT$(PadStr$, Padding) + IntStr$)
END IF
Hundreds = INT(LEN(IntStr$) / 3) '<--- number of 100s groups
Remain = LEN(IntStr$) - Hundreds * 3 '<--- not 100s group
IF Remain THEN
IF LEN(IntStr$) <= 3 THEN '<--- one 100s group
Remain = LEN(IntStr$) + 1
ELSE '<--- more than one 100s group
IntStr$ = LEFT$(IntStr$, Remain) + MidDlm$ + MID$(IntStr$, Remain + 1)
Remain = Remain + 3 + 1
END IF
ELSE
Remain = 3
END IF
FOR I = 1 TO Hundreds
IF Remain >= LEN(IntStr$) THEN EXIT FOR
IntStr$ = LEFT$(IntStr$, Remain) + MidDlm$ + MID$(IntStr$, Remain + 1)
Remain = Remain + 3 + 1 '<--- location of next 100s group
NEXT I
IF PadStr$ <> "0" THEN '<--- add front padding
IntStr$ = Minus$ + IntStr$
Padding = MaxLen - LEN(IntStr$)
IntStr$ = (FN.REPEAT$(" ", Padding) + IntStr$)
ELSE
IntStr$ = Minus$ + IntStr$
END IF
IF DecStr$ = "" THEN
NumStr$ = IntStr$
ELSE
NumStr$ = IntStr$ + DecStr$
END IF
FN.Fmt$ = NumStr$
EXIT FUNCTION
END IF
Padding = MaxLen - LEN(IntStr$)
IF PadStr$ = "0" THEN
Padding = Padding - 3
IntStr$ = (FN.REPEAT$(PadStr$, Padding) + IntStr$)
END IF
Hundreds = INT(LEN(IntStr$) / 3)
Remain = LEN(IntStr$) - Hundreds * 3
IF Remain THEN
IF LEN(IntStr$) <= 3 THEN
Remain = LEN(IntStr$) + 1
ELSE
IntStr$ = LEFT$(IntStr$, Remain) + MidDlm$ + MID$(IntStr$, Remain + 1)
Remain = Remain + 3 + 1' + Padding
END IF
ELSE
Remain = 3
END IF
FOR I = 1 TO Hundreds
IF Remain >= LEN(IntStr$) THEN EXIT FOR
IntStr$ = LEFT$(IntStr$, Remain) + MidDlm$ + MID$(IntStr$, Remain + 1)
Remain = Remain + 3 + 1
NEXT I
IF PadStr$ <> "0" THEN
Padding = MaxLen - LEN(IntStr$)
IntStr$ = (FN.REPEAT$(" ", Padding) + IntStr$)
ELSE
IntStr$ = " " + IntStr$
END IF
NumStr$ = IntStr$ + DecStr$
FN.Fmt$ = NumStr$
END FUNCTION
'