How to create an online calculator |
The calculation field feature in Simfatic Forms can be used to do complex calculations live.
This article shows you an example that breaks a complex formula and logic for a tax calculation. Note that the rules used are fictitious. You can assume any currency or any country.
Having said that, here are the rules of the land:
Here, we have the form:
It is always better to break a big formula into smaller ones.
Part I : Taxable IncomeTo calculate the taxable income, we have to deduct the 'contribution to retirement plan' from the total income.
taxable_income = total_income - retirement_plan_contribution
But, there is an upper limit to the retirement_plan_contribution. Simfatic Forms provides several functions. The min() function returns the minimum of the inputs. So, can make it this way:
taxable_income = total_income - min(retirement_plan_contribution,10000)
Now, there is a discriminating minimum income limit. We take the input from the 'Gender' radio button. Here is the updated formula:
taxable_income = total_income - min(retirement_plan_contribution,10000) - Gender
When we add a radio button or dropdown list to the formula, we get a new tab in the 'Value maps' tab of the formula editor. This is to map the selection to its numeric value.
For example, if the 'Gender' is Male, we need to use 100000 and for Female, 150000.
Value MapsWhen you are using a selection (radio group, dropdownlist) in a formula, you may have to map the selected value to a corresponding numeric value. Simfatic Forms' value map makes this easy. If your formula contains a selection, the value maps tab will have mapping for each of such inputs. Just provide the numeric value to be used in the calculation.
There is, an additional rule that Senior citizens also get the 150000 limit. So, we update the formula such that if not SeniorCitizen, take the Gender based limit. It SeniorCitizen, then always deduct 150000. Here is the updated formula:
taxable_income = total_income - min(retirement_plan_contribution,10000 ) - !SeniorCitizen * Gender - SeniorCitizen * 150000
Notice that the check box 'SeniorCitizen' returns 1 if checked and 0 if not checked.
Part II : Calculate TaxWe now has the final taxable income. Tax is 30% of the taxable income. Simfatic Forms has a function perc_of() to calculate percentage.
tax_before_deductions = perc_of(taxable_income,30)
To reach the tax payable, we need to deduct the tax rebates.
tax_payable = tax_before_deductions - perc_of( min( HouseRent ,100000),10) - perc_of( min( medical ,15000),20)
We have got the final tax amount. But there is a problem; the tax can become negative if the income is low ! I know no government that pays tax to the citizen
We can fix this by having the formula below
tax_displayed = min(tax_payable,0)
You can use hidden calculation fields to keep the intermediate results. In this calculation, taxable_income and tax_payable can be hidden; displaying only tax_displayed to the user.
During the design phase, keep the intermediate results visible so that you can see how the calculation goes. Once the calculations are working, you can make them hidden.
See Also:
Calculation field : function reference
|