checkers Posted March 17, 2012 Share Posted March 17, 2012 Hi there, I'm trying to create a land transfer tax calculator. The formula is to be as follows Up to and including $55,000.00 0.5% plus $55,000.01 to $400,000.00 1.0% plus Over $400,000.00 2.0% FOR EXAMPLE: A home with a consideration value of $500,000.01 MLTT Rate Calculation 0 to $55,000.00 55,000.00 x 0.005 = $275.00 $55,000.01 to $400,000.00 $344,999.99 x 0.01= $3,450.00 $400,000.01 to $500,000.00 $99,999.99 x 0.02= $2,000.00 Total MLTT= $5,725.00 what I have so far is <form method="GET" name="tax" action="http://www.abc.com/what-can-i-afford/"> <input type="hidden" name="comp_form" value="1"> <table cellpadding="2" cellspacing="0" border="0" width="100%" align="center"> <tr valign="top" bgcolor="#eeeeee"> <td align="right" width="150px">Home Cost</td> <td><input type="text" size="10" name="home_cost" value="<?php echo $home_cost; ?>"> (In Dollars)</td> </tr> <tr><td> </td></tr> <tr><td><input type="submit" value="Calculate"><br> <?php if ($comp_form) { print("<a href=\"" . $_SERVER['PHP_SELF'] . "\">Start Over</a><br>"); } ?><br></td> </tr> </table> <?php /* -----------------------------------------------*/ /* Affordability Calc */ /* Set Form DEFAULT values */ $home_cost = 0; /* Set the USER INPUT values */ if (isset($_REQUEST['comp_form'])) { $home_cost = $_REQUEST['home_cost']; if ($home_cost >= 55000) { $MLTT = 55000 * 0.005; if ( ($home_cost >= 55000.01) && ($home_cost <= 400000.01) ) { $MLTT = $MLTT + (($home_cost - 55000)*0.01); if ( ($home_cost >= 400000.01) && ($home_cost <= 500000) ) { $MLTT = $MLTT + (($home_cost - 400000.01)*0.02); } } echo $MLTT; } ?> <h2>MLTT is:<?php $MLTT ?></h2> <?php } ?> I'm only getting 275??? Please help as I'm unsure how to break up the home amount and perform each calculation Link to comment https://forums.phpfreaks.com/topic/259151-calculator-help-please/ Share on other sites More sharing options...
checkers Posted March 18, 2012 Author Share Posted March 18, 2012 **BUMP** Link to comment https://forums.phpfreaks.com/topic/259151-calculator-help-please/#findComment-1328802 Share on other sites More sharing options...
DavidAM Posted March 18, 2012 Share Posted March 18, 2012 [*]If the home cost less than 55,0000, you are not charging any tax (not related to the current complaint); [*]If the home is over 400,000, you are not charging the middle tier; [*]If the home is over 500,000, you are not charging the third tire; It would work correctly, like this: # $home_cost = $_REQUEST['home_cost']; $home_cost = 500000.01; if ($home_cost > 0) { $MLTT = min($home_cost, 55000) * 0.005; if ($home_cost > 55000) { $MLTT += min(($home_cost - 55000), (400000 - 55000)) * 0.01; if ($home_cost > 400000) { $MLTT += ($home_cost - 400000) * 0.02; } } } else { echo 'Are you sure you want the home if they have to PAY you to take it?!'; } printf('My Calc #1: %.3f' . PHP_EOL, $MLTT); As an alternative, you can put the values in an array, and then not have to rewrite your code if the tiers change: # $home_cost = $_REQUEST['home_cost']; $home_cost = 500000.01; $breaks = array( array('min' => 0, 'max' => 55000, 'rate' => 0.005), array('min' => 55000, 'max' => 400000, 'rate' => 0.01), array('min' => 400000, 'max' => -1, 'rate' => 0.02)); $MLTT = 0; foreach ($breaks as $vals) { if ($vals['max'] == -1) { /* edge case - the LAST entry should have -1 for the max */ $MLTT += ($home_cost - $vals['min']) * $vals['rate']; } else { $MLTT += min(($home_cost - $vals['min']), ($vals['max'] - $vals['min'])) * $vals['rate']; } } printf('My Calc #2: %.3f' . PHP_EOL, $MLTT); Link to comment https://forums.phpfreaks.com/topic/259151-calculator-help-please/#findComment-1328807 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.