INPUT "Limit Display: "; limit&&
round_total% = -1
round_total% = 0
LINE INPUT "Display in Scientific Notation? Y/N: "; ans$
snconvert% = -1
snconvert% = 0
LINE INPUT "Display in Dollars and Cents? Y/N: "; ans$
LINE INPUT "Display Results with Commas? Y/N: "; ans$
origb$ = stringmathb$
CALL stringmath
(stringmatha$
, operator$
, stringmathb$
, runningtotal$
, snconvert%
, round_total%
, show_rounding%
, comma_display%
, currency_display%
, limit&&
) IF stringmathb$
<> "invalid number" AND stringmathb$
<> "overflow" THEN runningtotal#
= VAL(origa$
) + VAL(origb$
) runningtotal#
= VAL(origa$
) - VAL(origb$
) runningtotal#
= VAL(origa$
) * VAL(origb$
) runningtotal#
= VAL(origa$
) / VAL(origb$
) PRINT "String Total: "; runningtotal$
IF operator$
= "=" THEN operator$
= "+" IF operator$
= "8" THEN operator$
= "*" orig_operator$ = operator$
SUB stringmath
(stringmatha$
, operator$
, stringmathb$
, runningtotal$
, snconvert%
, round_total%
, show_rounding%
, comma_display%
, currency_display%
, limit&&
) stringmathround$ = ""
IF limit&&
> 2147483640 THEN limit&&
= 2147483640 IF limit&&
= 0 THEN limit&&
= 70 ' Default.
IF RIGHT$(UCASE$(runningtotal$
), 1) = "R" THEN runningtotal$
= MID$(runningtotal$
, 1, LEN(runningtotal$
) - 1) 'Strip off rounding designation. ' Check running total. If S.N. convert to numeric for operations.
holdstringmathb$ = stringmathb$
stringmathb$ = runningtotal$
runningtotal$ = stringmathb$: stringmathb$ = holdstringmathb$: holdstringmathb$ = ""
' Check input number. If S.N. convert to numeric for operations.
GOSUB validate_string_number
GOSUB scientific_to_numeric
GOSUB validate_string_number
stringmathb$
= MID$(stringmathb$
, 2) n2sign$ = "-"
n2sign$ = ""
GOSUB limit_round_convert
IF stringmathb$
= "overflow" THEN runningtotal$ = n2sign$ + stringmathb$: n2sign$ = ""
IF stringmathround$
<> "" THEN runningtotal$
= runningtotal$
+ stringmathround$
PRINT "Validated: "; runningtotal$
GOSUB validate_string_number
PRINT "Validated: "; stringmathb$
IF runningtotal$
<> "" THEN stringmatha$
= runningtotal$
string_add_subtract:
IF INSTR(stringmatha$
, ".") <> 0 THEN ' Evaluate sum for decimal fraction. sumplace&
= LEN(stringmatha$
) - INSTR(stringmatha$
, ".") stringmatha$
= MID$(stringmatha$
, 1, INSTR(stringmatha$
, ".") - 1) + MID$(stringmatha$
, INSTR(stringmatha$
, ".") + 1) ' Strip out decimal IF INSTR(stringmathb$
, ".") <> 0 THEN ' Evaluate number for decimal fraction. numplace&
= LEN(stringmathb$
) - INSTR(stringmathb$
, ".") stringmathb$
= MID$(stringmathb$
, 1, INSTR(stringmathb$
, ".") - 1) + MID$(stringmathb$
, INSTR(stringmathb$
, ".") + 1) ' Strip out decimal IF sumplace&
> numplace&
THEN addsubplace&
= sumplace&
ELSE addsubplace&
= numplace&
IF sumplace&
> addsubplace&
THEN stringmatha$
= stringmatha$
+ STRING$(sumplace&
- addsubplace&
, "0") stringmatha$
= stringmatha$
+ STRING$(addsubplace&
- sumplace&
, "0") IF numplace&
> addsubplace&
THEN stringmathb$
= stringmathb$
+ STRING$(numplace&
- addsubplace&
, "0") stringmathb$
= stringmathb$
+ STRING$(addsubplace&
- numplace&
, "0") END IF ' END Decimal evaluations.
IF LEFT$(stringmatha$
, 1) = "-" THEN sign_input$
= "-" ELSE sign_input$
= "+" IF LEFT$(stringmathb$
, 1) = "-" THEN sign_total$
= "-" ELSE sign_total$
= "+"
addsubsign% = 0
operator$ = "+"
operator$ = "-"
IF VAL(stringmathb$
) > VAL(stringmatha$
) THEN SWAP stringmatha$
, stringmathb$: addsubsign%
= -1 operator$ = "-"
operator$ = "+"
addsubsign% = -1
stringmathb$
= STRING$(LEN(stringmatha$
) - LEN(stringmathb$
), "0") + stringmathb$
stringmatha$
= STRING$(LEN(stringmathb$
) - LEN(stringmatha$
), "0") + stringmatha$
addsubx1$ = ""
addsubx1%
= VAL(MID$(stringmatha$
, addsubii&
, 1)) + VAL(MID$(stringmathb$
, addsubii&
, 1)) + addsubcarry%
IF addsubx1%
> 9 THEN addsubx1%
= addsubx1%
- 10: addsubcarry%
= 1 ELSE addsubcarry%
= 0 IF addsubcarry%
THEN addsubx1$
= "1" + addsubx1$: addsubcarry%
= 0 addsubx1%
= VAL(MID$(stringmatha$
, addsubii&
, 1)) - VAL(MID$(stringmathb$
, addsubii&
, 1)) + addsubcarry%
IF addsubx1%
< 0 THEN addsubx1%
= addsubx1%
+ 10: addsubcarry%
= -1 ELSE addsubcarry%
= 0 addsubx1$
= MID$(addsubx1$
, 2) addsubx1$ = "0": addsubsign% = 0
IF addsubcarry%
THEN addsubx1$
= "-" + addsubx1$: addsubcarry%
= 0
IF LEFT$(addsubx1$
, 1) = "-" THEN addsubx1$
= MID$(addsubx1$
, 2) ELSE addsubx1$
= "-" + addsubx1$
stringmatha$ = addsubx1$: addsubx1$ = ""
stringmathb$ = stringmatha$: stringmatha$ = ""
stringmathb$
= MID$(stringmathb$
, 2) n2sign$ = "-"
n2sign$ = ""
GOSUB limit_round_convert
runningtotal$ = n2sign$ + stringmathb$: n2sign$ = ""
string_multiply:
fac1$ = stringmatha$: fac2$ = stringmathb$ ' Make numbers whole numbers and remove any - sign.
IF INSTR(fac2$
, ".") <> 0 THEN m_decimal_places&
= m_decimal_places&
+ LEN(fac2$
) - INSTR(fac2$
, "."): fac2$
= MID$(fac2$
, 1, INSTR(fac2$
, ".") - 1) + MID$(fac2$
, INSTR(fac2$
, ".") + 1) FOR m_i&
= LEN(fac2$
) TO 1 STEP -1 ' Multiply each charter top and bottom. m_k& = m_l&
m_x2$
= MID$(fac2$
, m_i&
, 1) m_x1$
= MID$(fac1$
, m_j&
, 1) m_t& = 0: m_xproduct$ = "": m_carry% = 0
DO ' Add multiplied characters together. m_x3$
= MID$(m_add$
, LEN(m_add$
) - m_t&
, 1) m_x4$
= MID$(m_product$
, LEN(m_product$
) - m_t&
, 1) IF m_carry%
THEN m_xproduct$
= "1" + m_xproduct$
m_g%
= VAL(m_x3$
) + VAL(m_x4$
) + m_carry%
IF m_g%
>= 10 THEN m_g%
= m_g%
- 10: m_carry%
= 1 ELSE m_carry%
= 0 m_t& = m_t& + 1
m_product$ = m_xproduct$: m_xproduct$ = ""
m_k& = m_k& + 1 ' Adds trailing zeros multiplication
m_l& = m_l& + 1 ' Used to reset value for m_k& adding one trailing zer for each loop.
fac1$ = "": fac2$ = "": m_l& = 0: m_k& = 0: m_t& = 0
IF m_decimal_places&
> LEN(m_product$
) THEN m_product$
= STRING$(m_decimal_places&
- LEN(m_product$
), "0") + m_product$
' Add any leading zeros to a decimal. Ex: .02 * .01 is factored as 002. It needs one leading zero before adding the decimal point, .0002. IF m_decimal_places&
AND m_product$
<> "0" THEN ' Replace any decimal point. m_product$
= MID$(m_product$
, 1, LEN(m_product$
) - m_decimal_places&
) + "." + MID$(m_product$
, LEN(m_product$
) - m_decimal_places&
+ 1) DO UNTIL LEFT$(m_product$
, 1) <> "0" ' Remove leading zeros. m_product$
= MID$(m_product$
, 2) DO UNTIL RIGHT$(m_product$
, 1) <> "0" ' Remove trailing zeros in a decimal sum. m_product$
= MID$(m_product$
, 1, LEN(m_product$
) - 1) IF m_product$
= "" THEN m_product$
= "0": m_sign%
= 0 IF RIGHT$(m_product$
, 1) = "." THEN m_product$
= MID$(m_product$
, 1, LEN(m_product$
) - 1) ' Remove decimal from the end of an integer total. stringmathb$
= m_product$: m_product$
= "":
GOSUB limit_round_convert
runningtotal$ = stringmathb$: stringmathb$ = ""
IF m_sign%
THEN runningtotal$
= "-" + runningtotal$: m_sign%
= 0
operationdivision% = -1
divbuffer&
= LEN(stringmathb$
) - LEN(stringmatha$
) IF divbuffer&
< 0 THEN divbuffer&
= 0 d2dividend$ = stringmatha$
d1divisor$ = stringmathb$
IF LEFT$(d1divisor$
, 1) = "-" THEN divsign%
= -1: d1divisor$
= MID$(d1divisor$
, 2) divsign% = 0
divsign% = -1
d2dividend$
= MID$(d2dividend$
, 2) d1divisor$
= MID$(d1divisor$
, 1, LEN(d1divisor$
) - 1) ' Strip off trailing zeros divplace&
= LEN(d1divisor$
) - INSTR(d1divisor$
, ".") d1divisor$
= MID$(d1divisor$
, 1, INSTR(d1divisor$
, ".") - 1) + MID$(d1divisor$
, INSTR(d1divisor$
, ".") + 1) ' Strip off decimal point. d1divisor$
= MID$(d1divisor$
, 2) ' Strip off leading zeros for divisors smaller than .1
d2dividend$
= d2dividend$
+ STRING$(divplace&
- LEN(d2dividend$
) - INSTR(d2dividend$
, "."), "0") ' Add any zeros based on the length of dividend at decimal - length of divisor at decimal. If less than zero, nothing added. divplace2&
= INSTR(d2dividend$
, ".") d2dividend$
= MID$(d2dividend$
, 1, LEN(d2dividend$
) - 1) ' Strip off trailing zeros d2dividend$
= MID$(d2dividend$
, 1, INSTR(d2dividend$
, ".") - 1) + MID$(d2dividend$
, INSTR(d2dividend$
, ".") + 1) ' Strip off decimal point. d2dividend$
= d2dividend$
+ STRING$(divplace&
, "0") ' Add any zeros based on the length of dividend at decimal - length of divisor at decimal. If less than zero, nothing added. divplace& = 0
divremainder&
= divremainder&
+ 1: divremainder$
= divremainder$
+ MID$(d2dividend$
, divremainder&
, 1) divcarry& = divcarry& + 1
IF divcarry&
= 1 THEN divplace3&
= divremainder&
- 1 IF divcarry&
> limit&&
+ 1 + divbuffer&
THEN divremainder$ = divremainder$ + "0" ' No more digits to bring down.
quotient$ = quotient$ + "0"
stringmatha$
= LTRIM$(STR$(div_i%
)): stringmathb$
= d1divisor$
m_product$
= "":
GOSUB string_multiply
tempcutd$ = divremainder$ ' divremainder$ can be 00 or other leading zero values.
tempcutd$
= MID$(tempcutd$
, 2) stringmatha$
= LTRIM$(STR$(div_i%
)): stringmathb$
= d1divisor$
m_product$
= "":
GOSUB string_multiply
operator$ = "-"
stringmatha$ = divremainder$
stringmathb$ = m_product$
GOSUB string_add_subtract
divremainder$ = stringmatha$
operator$ = "/"
IF divplace&
= 0 AND divplace2&
= 0 THEN divplace&
= divplace3&
IF divplace2&
THEN divplace&
= divplace&
+ divplace2&
- 1 IF quotient$
= "" THEN divplace&
= 0 ' dividend is zero. quotient$
= MID$(quotient$
, 1, divplace&
) + "." + MID$(quotient$
, divplace&
+ 1) quotient$
= MID$(quotient$
, 1, LEN(quotient$
) - 1) ' Strip off trailing zeros IF RIGHT$(quotient$
, 1) = "." THEN quotient$
= MID$(quotient$
, 1, LEN(quotient$
) - 1) ' Strip off abandoned decimal. quotient$
= MID$(quotient$
, 2) ' Strip off leading zeros IF quotient$
= "" THEN quotient$
= "0": divsign%
= 0 operationdivision% = 0
stringmathb$
= quotient$: quotient$
= "":
GOSUB limit_round_convert
runningtotal$ = stringmathb$: stringmathb$ = ""
IF divsign%
THEN runningtotal$
= "-" + runningtotal$
IF stringmathround$
<> "" THEN runningtotal$
= runningtotal$
+ stringmathround$
validate_string_number:
vsn_negcnt& = 0: vsn_poscnt& = 0: vsn_depresent& = 0: decimalcnt& = 0: vsn_numberpresent& = 0: vsn_zerospresent& = 0
IF LEFT$(stringmathb$
, 1) = "-" THEN stringmathb$
= MID$(stringmathb$
, 2): sm_sign$
= "-" ELSE sm_sign$
= "" validatenum$
= MID$(UCASE$(stringmathb$
), sm_i&
, 1) IF vsn_depresent&
THEN vsn_poscnt&
= vsn_poscnt&
+ 1 ELSE stringmathb$
= "invalid number":
RETURN IF vsn_depresent&
THEN vsn_negcnt&
= vsn_negcnt&
+ 1 ELSE stringmathb$
= "invalid number":
RETURN vsn_numberpresent& = -1
vsn_depresent& = vsn_depresent& + 1
IF decimalcnt&
= 0 AND sm_i&
<> 2 OR vsn_depresent&
> 1 OR vsn_numberpresent&
= 0 OR vsn_negcnt&
> 1 OR vsn_poscnt&
> 1 OR vsn_negcnt&
= 1 AND vsn_poscnt&
>= 1 THEN vsn_numberpresent&
= 0:
EXIT FOR vsn_numberpresent& = 0
MID$(stringmathb$
, sm_i&
, 1) = "e" ' Standardize decimalcnt& = decimalcnt& + 1
IF decimalcnt&
= 0 THEN stringmathb$
= MID$(stringmathb$
, 1, 1) + "." + MID$(stringmathb$
, 2) ' Standardize "." IF vsn_numberpresent&
= 0 OR vsn_negcnt&
= 1 AND vsn_poscnt&
= 1 OR decimalcnt&
> 1 OR INSTR(stringmathb$
, ".") <> 2 THEN stringmathb$
= "invalid number":
RETURN vsn_depresent&
= INSTR(stringmathb$
, "e") sm_x$
= MID$(stringmathb$
, vsn_depresent&
+ 1, 1) ' Standardize exponent "+" these two lines. IF sm_x$
<> "+" AND sm_x$
<> "-" THEN stringmathb$
= MID$(stringmathb$
, 1, vsn_depresent&
) + "+" + MID$(stringmathb$
, vsn_depresent&
+ 1) IF MID$(stringmathb$
, vsn_depresent&
+ 2, 1) = "0" THEN IF MID$(stringmathb$
, vsn_depresent&
+ 3, 1) <> "" THEN stringmathb$
= "invalid number":
RETURN ' No leading zeros allowed in exponent notation. jjed&
= INSTR(stringmathb$
, "e") ' Get position of notation. valexpside$
= MID$(stringmathb$
, jjed&
) ' These two lines break up into number and notation stringmathb$
= MID$(stringmathb$
, 1, jjed&
- 1) ' stringmathb$ is +- single digit whole number, decimal point and decimal number. valexpside$ is notation, sign and exponent. DO UNTIL RIGHT$(stringmathb$
, 1) <> "0" ' Remove any trailing zeros for number. Example 1.0d3 or 1.0000d3, etc. stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1) stringmathb$ = "0.e+0" ' Handles all types of zero entries.
stringmathb$
= "invalid number":
RETURN stringmathb$ = sm_sign$ + stringmathb$ + valexpside$
validatenum$
= MID$(stringmathb$
, sm_i&
, 1) decimalcnt& = decimalcnt& + 1
vsn_zerospresent& = -1
vsn_numberpresent& = -1
stringmathb$
= "invalid number":
RETURN IF decimalcnt&
> 1 OR vsn_negcnt&
> 1 OR vsn_poscnt&
> 1 OR vsn_negcnt&
>= 1 AND vsn_poscnt&
>= 1 THEN stringmathb$
= "invalid number":
RETURN IF RIGHT$(stringmathb$
, 1) = "." THEN stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1) DO UNTIL LEFT$(stringmathb$
, 1) <> "0" ' Strip off any leading zeros. stringmathb$
= MID$(stringmathb$
, 2) stringmathb$ = sm_sign$ + stringmathb$
DO UNTIL RIGHT$(stringmathb$
, 1) <> "0" ' Strip off any trailing zeros in a decimal. stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1) IF RIGHT$(stringmathb$
, 1) = "." THEN stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1) IF vsn_numberpresent&
= 0 THEN stringmathb$ = "0"
stringmathb$ = "invalid number"
' Convert to commas, currency, S.N., etc.
sm_converter:
' Add in commas.
comma_placement:
sm_i& = 0: sm_j& = 0: sm_seed& = 0
sm_seed&
= LEN(temp_stringmathb1$
) MOD 3:
IF sm_seed&
= 0 THEN sm_seed&
= 3sm_m1&
= LEN(temp_stringmathb1$
)sm_m2&
= (LEN(temp_stringmathb1$
) - 1) \
3sm_replace$
= SPACE$(sm_m1&
+ sm_m2&
) MID$(sm_replace$
, sm_j&
+ 1, sm_seed&
+ 1) = MID$(temp_stringmathb1$
, sm_i&
+ 1, sm_seed&
) + "," sm_i& = sm_i& + sm_seed&: sm_j& = sm_j& + sm_seed& + 1: sm_seed& = 3
sm_replace$
= RTRIM$(sm_replace$
) stringmathb$
= MID$(sm_replace$
, 1, LEN(sm_replace$
) - 1) stringmathb$ = sm_replace$
sm_replace$ = "": temp_stringmathb1$ = ""
' Validate comma entry.
comma_validation:
stringmathb$ = "invalid number" ' Decimal part has comma or entry is all commas.
sm_j% = sm_j% + 1
IF MID$(temp_stringmathb1$
, sm_i&
, 1) <> "," THEN stringmathb$
= "invalid number":
EXIT FOR sm_j% = 0
IF stringmathb$
<> "invalid number" THEN stringmathb$ = sm_sign$ + temp_stringmathb1$ + temp_stringmathb2$
temp_stringmathb1$ = "": temp_stringmathb2$ = "": sm_i& = 0: sm_j% = 0: sm_sign$ = "": sm_dollar$ = ""
comma_removal:
sm_i& = 0: sm_j& = 0: sm_seed& = 0
sm_i&
= INSTR(sm_seed&
+ 1, stringmathb$
, ",") MID$(sm_replace$
, sm_j&
+ 1, sm_i&
- sm_seed&
+ 1) = MID$(stringmathb$
, sm_seed&
+ 1, sm_i&
- sm_seed&
- 1) sm_j& = sm_j& + sm_i& - sm_seed& - 1
sm_seed& = sm_i&
stringmathb$
= RTRIM$(sm_replace$
) + MID$(stringmathb$
, sm_seed&
+ 1): sm_replace$
= ""
comma_prep:
IF LEFT$(stringmathb$
, 1) = "-" THEN stringmathb$
= MID$(stringmathb$
, 2): sm_sign$
= "-" temp_stringmathb1$ = stringmathb$: stringmathb$ = ""
temp_stringmathb2$
= MID$(temp_stringmathb1$
, INSTR(temp_stringmathb1$
, ".")) ' Decimal part temp_stringmathb1$
= MID$(temp_stringmathb1$
, 1, INSTR(temp_stringmathb1$
, ".") - 1) ' Non-decimal partIF LEFT$(temp_stringmathb1$
, 1) = "$" THEN temp_stringmathb1$
= MID$(temp_stringmathb1$
, 2): sm_dollar$
= "$"
currency_validate:
sm_dollar$ = "$"
currency_convert:
IF INSTR(stringmathb$
, ".") = 0 THEN stringmathb$
= stringmathb$
+ ".00" IF RIGHT$(stringmathb$
, 1) = "." THEN stringmathb$
= stringmathb$
+ "00" IF MID$(stringmathb$
, LEN(stringmathb$
) - 2, 1) <> "." THEN stringmathb$
= stringmathb$
+ "0" IF MID$(stringmathb$
, LEN(stringmathb$
) - 2, 1) <> "." THEN stringmathb$
= "invalid number":
RETURN stringmathb$ = sm_sign$ + "$" + stringmathb$
numeric_to_scientific:
IF LEFT$(stringmathb$
, 1) = "-" THEN stringmathb$
= MID$(stringmathb$
, 2): n2sign$
= "-" IF INSTR(stringmathb$
, ".") = 0 THEN exponentvalue&&
= LEN(stringmathb$
) - 1 ELSE exponentvalue&&
= INSTR(stringmathb$
, ".") - 2 ' Exponent is one less than number of digits for whole number an two less than the placement of the decimal point for a fraction. stringmathb$
= MID$(stringmathb$
, 1, INSTR(stringmathb$
, ".") - 1) + MID$(stringmathb$
, INSTR(stringmathb$
, ".") + 1) DO UNTIL LEFT$(stringmathb$
, 1) <> "0" ' Remove leading zeros to consider rounding. stringmathb$
= MID$(stringmathb$
, 2) exponentvalue&& = exponentvalue&& - 1
esign$ = "-"
esign$ = "+"
stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1)IF stringmathb$
= "" THEN stringmathb$
= "0": esign$
= "+": exponentvalue&&
= 0 stringmathb$
= LEFT$(stringmathb$
, 1) + "." + MID$(stringmathb$
, 2)IF stringmathb$
= "0." THEN n2sign$
= "": esign$
= "+" stringmathb$
= stringmathb$
+ "e" + esign$
+ LTRIM$(STR$(ABS(exponentvalue&&
))) ' S.N formed here.IF stringmathb$
<> "overflow" THEN stringmathb$ = n2sign$ + stringmathb$
n2sign$ = "": esign$ = "": exponentvalue&& = 0
scientific_to_numeric:
IF MID$(stringmathb$
, INSTR(stringmathb$
, "e") + 2) = "0" THEN ' The numeric value is the number without the zero exponent. stringmathb$
= MID$(stringmathb$
, 1, INSTR(stringmathb$
, "e") - 1) IF RIGHT$(stringmathb$
, 1) = "." THEN stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1) IF LEFT$(stringmathb$
, 1) = "-" THEN stn_sign$
= "-": stringmathb$
= MID$(stringmathb$
, 2) stringmathb$
= MID$(stringmathb$
, 1, INSTR(stringmathb$
, ".") - 1) + MID$(stringmathb$
, INSTR(stringmathb$
, ".") + 1) ' Remove decimal point. stn_i&
= INSTR(stringmathb$
, "e") - 1 ' Length of the numric part. stringmathb$
= "." + STRING$(VAL(MID$(stringmathb$
, stn_i&
+ 3)) - 1, "0") + MID$(stringmathb$
, 1, stn_i&
) ' Decimal point followed by exponent value in zeros added in front of numeric part. IF stn_i&
- 1 > VAL(MID$(stringmathb$
, stn_i&
+ 3)) THEN stn_point$
= "." ' - 1 for decimal place. Ex 2.034d+2 is 2034 here where 3 places to the right . could be moved before . disappears. > so no trailing decimal results. stringmathb$
= MID$(MID$(stringmathb$
, 1, stn_i&
), 1, VAL(MID$(stringmathb$
, stn_i&
+ 3)) + 1) + stn_point$
+ MID$(MID$(stringmathb$
, 1, stn_i&
), VAL(MID$(stringmathb$
, stn_i&
+ 3)) + 2, stn_i&
- VAL(MID$(stringmathb$
, stn_i&
+ 3)) - 1) + STRING$(VAL(MID$(stringmathb$
, stn_i&
+ 2)) - (stn_i&
- 1), "0")IF stringmathb$
= "0" THEN stn_sign$
= "" stringmathb$ = stn_sign$ + stringmathb$
stn_sign$ = "": stn_point$ = ""
limit_round_convert:
' Try SN if whole number is too large (as it may be trailing zeros) or decimal is beyond limit.
GOSUB numeric_to_scientific
' Retry as S.N. IF LEN(stringmathb$
) > limit&&
+ 3 THEN ' Needs rounding. exponentvalue&&
= VAL(MID$(snotation$
, 2)) ' Get positive or negative sign. '''IF LEN(stringmathb$) + LEN(snexponent$) > limit&& + 1 AND exponentvalue&& >= limit&& THEN BEEP
IF exponentvalue&&
>= limit&&
THEN stringmathb$
= MID$(stringmathb$
, 1, exponentvalue&&
+ 3) stringmathb$
= MID$(stringmathb$
, 1, limit&&
- LEN(snexponent$
) + 2) GOSUB string_rounding_method
stringmathb$
= "1." + MID$(stringmathb$
, 4) ' Add one to the exponent.
round_x$
= CHR$(ASC(MID$(snexponent$
, round_i&
, 1)) + 1) IF round_x$
<> CHR$(47) THEN ' Decimal point + 1. Ignore. MID$(snexponent$
, round_i&
, 1) = "0": carry$
= "1" MID$(snexponent$
, round_i&
, 1) = round_x$: carry$
= "":
EXIT FOR snexponent$ = carry$ + snexponent$: carry$ = ""
stringmathb$
= stringmathb$
+ MID$(snotation$
, 1, 2) + snexponent$
stringmathb$ = "overflow"
exponentvalue&& = 0
stringmathb$
= MID$(stringmathb$
, 1, limit&&
+ 2) GOSUB string_rounding_method
stringmathb$
= MID$(stringmathb$
, 1, limit&&
+ 1) IF show_rounding%
THEN stringmathround$
= "r" stringmathb$ = "overflow"
stringmathb$
= MID$(stringmathb$
, 1, limit&&
+ 1) stringmathb$
= MID$(stringmathb$
, 1, limit&&
+ 2) GOSUB string_rounding_method
IF LEN(stringmathb$
) > limit&&
+ lrc_decimalpoint&
THEN ' Ex: limit&& = 4 9999.9 1.e+4 GOSUB numeric_to_scientific
IF LEN(stringmathb$
) > limit&&
+ lrc_decimalpoint&
THEN stringmathb$
= "overflow"
replace_decimal:
addsubx1$
= STRING$(addsubplace&
- LEN(addsubx1$
), "0") + addsubx1$
addsubx1$
= MID$(addsubx1$
, 1, LEN(addsubx1$
) - addsubplace&
) + "." + MID$(addsubx1$
, LEN(addsubx1$
) - addsubplace&
+ 1) DO UNTIL RIGHT$(addsubx1$
, 1) <> "0" ' Remove trailing zeros in a decimal sum. addsubx1$
= MID$(addsubx1$
, 1, LEN(addsubx1$
) - 1) addsubplace& = addsubplace& - 1
IF RIGHT$(addsubx1$
, 1) = "." THEN addsubx1$
= MID$(addsubx1$
, 1, LEN(addsubx1$
) - 1) ' Number is now an integer.
string_rounding_method:
IF INSTR(stringmathb$
, ".") THEN lrc_decimalpoint&
= 1 ELSE lrc_decimalpoint&
= 0 round_x$
= CHR$(ASC(MID$(stringmathb$
, round_i&
, 1)) + 1) IF round_x$
<> CHR$(47) THEN ' Decimal point + 1. Ignore. MID$(stringmathb$
, round_i&
, 1) = "0": carry$
= "1" MID$(stringmathb$
, round_i&
, 1) = round_x$: carry$
= "":
EXIT FOR stringmathb$
= carry$
+ MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1): carry$
= "" IF show_rounding%
THEN stringmathround$
= "R" stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1) IF show_rounding%
THEN stringmathround$
= "r"
stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1) IF stringmathb$
= "" OR stringmathb$
= "." THEN stringmathb$
= "0": lrc_decimalpoint&
= 0 stringmathb$
= MID$(stringmathb$
, 1, LEN(stringmathb$
) - 1): lrc_decimalpoint&
= 0