redarrow Posted February 23, 2007 Share Posted February 23, 2007 how do i code this properly as you can see i use floor becouse of the result of 13.3333333333333 how is it done with the added 33 pence on the end cheers. <?php define("INTREST",00.05); $price=100; $result=$price*INTREST; $year_intrest=$result*12; $total=$price+$year_intrest; $total_payment_monthly=$total/12; echo "Dear cutomer your product cost: $price <br> Your current intrest is: ".INTREST."% <br> The total 12 month intrest is: $year_intrest <br> The total price is: $total <br> Please pay: ".floor($total_payment_monthly)." monthly"; ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 23, 2007 Share Posted February 23, 2007 The word is interest. What is the problem with the code? What is the result, and expected result?q Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2007 Share Posted February 23, 2007 <?php define("INTREST",00.05); $price=100; $result=$price*INTREST; $year_intrest=$result*12; $total=$price+$year_intrest; $total_payment_monthly=number_format(($total/12),2); echo "Dear cutomer your product cost: $price <br> Your current intrest is: ".INTREST."% <br> The total 12 month intrest is: $year_intrest <br> The total price is: $total <br> Please pay: $total_payment_monthly monthly"; ?> You do realize that those payment calculations are wrong? You can't simply figure the interest on the priciple for a year and then divide by 12. There was a post here a few days ago from someone looking for a proper formula and I gave a solution - see below: <html> <head></head> <body> <?php if ($_POST['principle']) { $principle = $_POST['principle']; $months = $_POST['months']; $apr = $_POST['apr']; $rate = $apr/12/100; echo "Rate (per period): {$rate}<br>"; $payment = ( $rate + $rate/( pow((1+$rate),$months)-1) ) * $principle; echo "Payment: " . $payment; } ?> <form action="<? PHP_SELF ?>" method="POST"> Principle: <input type="text" name="principle" value="<?=$principle?>"><br> Months: <input type="text" name="months" value="<?=$months?>"><br> Apr: <input type="text" name="apr" value="<?=$apr?>"><br> <input type="submit" name="submit"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted February 23, 2007 Share Posted February 23, 2007 If you are just looking to round it use the round function. E.g: $float = 1.999999999999999999999 $float = round ($float, 2); echo $float; 2 = the number of places to round to. Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 23, 2007 Author Share Posted February 23, 2007 total payment monthly needs to be 13.33 not 13.333333333 so i used ceil but that not correct so what do i do cheers. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2007 Share Posted February 23, 2007 total payment monthly needs to be 13.33 not 13.333333333 so i used ceil but that not correct so what do i do cheers. You already have two solutions posted above: number_format() and round() But, as i also stated your calculations are not correct because interest is calculated each period. Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 23, 2007 Author Share Posted February 23, 2007 brake your code down and code functions so i understand it correctly please cheers. pow? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2007 Share Posted February 23, 2007 Which code? The code to fix your problem or the correct interest calculation. You are the one needing help, don't make me do all the work. If you are inquiring concerning the interest calculation then there is really nothing to explain. There are four variables involved: $principle: the loan amount $months: number of months (or periods) to pay off the loan $apr: Annual percentage rate Then you calculate the period rate, In this case it is apr / 12 months (I also divide by 100 because I assumed the rate would be entered as 5 instead of .05). $rate = $apr/12/100; Then the formula to calculate the payment per period in order to pay off the principle over the lifetime of the loan is this: $payment = ( $rate + $rate/( pow((1+$rate),$months)-1) ) * $principle; There are pleny of sites out there on how to properly calculate numbers related to loans, just do a Google search. To illustrate the issue, let's suppose you have a $100 loan to be paid in two payments over a year and the interest rate is %10. If you assumed that the total interest would be $10, you would be wrong. The interest is caluculated each period. So at the first payment the interest would be (APR/periods)*principle or (.10/2)*100 = $5. Assuming you paid $55 (1/2 the principle plus the interest) the final payment would be $52.50 because the principle would now be only $50. So the interest would be calculated as thus: (.10/2)*50 = $2.50 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 24, 2007 Share Posted February 24, 2007 ... There are four variables involved: $principle: the loan amount $months: number of months (or periods) to pay off the loan $apr: Annual percentage rate There are also 3 types of programmer, - Those that can count - those that can't. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Barand, everyone knows there are 10 types of people, gosh. Quote Link to comment Share on other sites More sharing options...
cmgmyr Posted February 24, 2007 Share Posted February 24, 2007 pow is power: http://us2.php.net/manual/en/function.pow.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 24, 2007 Share Posted February 24, 2007 ... There are four variables involved: $principle: the loan amount $months: number of months (or periods) to pay off the loan $apr: Annual percentage rate There are also 3 types of programmer, - Those that can count - those that can't. Well, there were 4 variables when I started to write that response. But, then I realized that my variable $rate was a derivative of $apr and was not really a "unique" variable so I removed it. And, since we are pointing out mistakes, you should have stated "There are also three types of programmers..." It's all good! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.