Trying a custom handler for several math fields

Mark Wieder mwieder at ahsoftware.net
Sun Sep 15 19:15:20 EDT 2013


Vaughn-

Sunday, September 15, 2013, 12:40:03 PM, you wrote:

>    put fld "Loan1" + fld "Loan2" into fld "Loan Balance"
>    put fld "Vehicle Selling Price" - fld "Buyers Offer Discount" into fld
> "Net Offer Price"
>    put fld "Net Offer Price" + fld "Buyers Options Cost" - fld "Factory
> Allowance" into fld "Sum of Offer"
>    put fld "Sum of Offer" - fld "Buyers Accepted Trade" into fld "Net Amt
> after Payoff"
>    put fld "Net Amt after Payoff" - fld "Buyers Down PMT." into fld
> "Balance Before Finance"
>    put fld "Balance Before Finance" + fld "Tax" + fld "Plates" + fld "Doc
> Fees" + fld "Options Other" into fld "Amount of Loan"
>    put fld "Loan Term in Months" * fld "Monthly Payment" into fld "Sum of
> Loan at Term"
>    put fld "Amount of Loan" - fld "Sum of Loan at Term" into fld "Interest
> Paid at Term"

This is not how I would approach this, primarily because of the
problem, as you have noted, of having to convert back and forth
between the displayed dollar sign and the calculation values. Rather,
I would separate out the business logic that does the calculations
from the presentation logic that displays the results. Using your
example above, that would come out something like

-- strip any formatting characters
function AmountOf pTextFromField
  replace "$" with empty in pTextFromField
  replace "," with empty in pTextFromField
  return pTextFromField
end AmountOf

function Currency pNumber
  put "$" before pNumber
  return pNumber
end Currency

-- get the user input
put AmountOf(field "Loan1") into tLoan1
put AmountOf(field "Loan2") into tLoan2
put AmountOf(field "Vehicle Selling Price") into tVehicleSellingPrice
put AmountOf(field "Buyers Offer Discount") into tBuyersOfferDiscount
put AmountOf(field "Buyers Accepted Trade") into tBuyersAcceptedTrade
put AmountOf(field "Buyers Down PMT") into tBuyersDownPMT
put AmountOf(field "Tax") into tTax
put AmountOf(field "Plates") into tPlates
put AmountOf(field "Doc Fees") into tFees
put AmountOf(field "Options Other") into tOptionsOther
put AmountOf(field "Loan Term In Months") into tLoanTermInMonths
put AmountOf(field "Monthly Payment") into tMonthlyPayment

-- do the calculations
put tLoan1 + tLoan2 into tNetOfferPrice
put tVehicleSellingPrice - tBuyersOfferDiscount into tNetOfferPrice
put tNetOfferPrice + tBuyersAcceptedTrade into tNetAfterPayoff
put tNetAfterPayoff - tBuyersDownPMT into tBalanceBeforeFinance
put tBalanceBeforeFinance + tTax + tPlates + tFees + tOptionsOther \
into tAmountOfLoan
put tLoanTermInMonths * tMonthlyPayment into tSumOfLoanAtTerm
put tAmountOfLoan - tSumOfLoanAtTerm into tInterestPaidAtTerm

-- now display the formatted output
put Currency(tNetOfferPrice) into field "Net Offer Price"
put Currency(tSumOfOffer) into field "Sum of Offer"
put Currency(tNetAfterPayoff) into field "Net Amount after Payoff"
put Currency(tBalanceBeforeFinance) into field "Balance Before Finance"
put Currency(tAmountOfLoan) into field "Amount of Loan"
put Currency(tSomeOfLoanAtTerm) into field "Sum of Loan at Term"
put Currency(tInterestPaidAtTerm) into field "Interest Paid at Term"

-- 
-Mark Wieder
 mwieder at ahsoftware.net





More information about the use-livecode mailing list