Support Forums

Full Version: A little help with my script would we appreciated.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey im making a script that calculates compound interest and everything should be working fine but when i run the script the answer is incorrect, i know the math is not wrong. Possibly the $I is automatically rounding off to a whole number (it is usually a decimal).

Anyway any help or tips would be appreciated.

Code:
<?php
/* This is the amount of money.
$P = $_POST["element_1"];
/* This is the % of interest (e.g. 18% is 0.18).
$I = $_POST["element_2"];
/* This is the time range.
$T = $_POST["element_3"];
/* This is to tell of any rests (e.g. monthly = 12, weekly = 52 and none = 1)
$K = $_POST["element_4"];
/* This is for the function below, its just T times K.
$N = $T*$K;

/* Heres the main function to find the compound interest.
$A = $P*(1+($I/$K))^$N;

print "Compound Interest is $A";
?>

Peace,

iPhish.
The following links should provide you with a solution to your problem.

http://se2.php.net/manual/en/language.types.integer.php
http://se2.php.net/manual/en/function.round.php

Next to that rounding can also be done in this way (example taken from "Recipes a problem solution approach")

PHP Code:
<?php
//Let's say you live in Canada and want to add GST tax to your amount.
$thenumber 9.99 1.07;
//If you simply outputted this, the value would be skewed.
echo "$" $thenumber "<br />"//Outputs $10.6893
//In order to show the value as a dollar amount, you can do this:
echo "$" sprintf ("%.2f"$thenumber); //Outputs $10.69
?>

Using the function round (taken from:"http://php.about.com/od/learnphp/ss/round_num_PHP.htm").

PHP Code:
echo round(3.141592653);
// Value would be 3.142 
The number "3.14159265" here would refer to the variable you have set. The number behind the "," in this case 3 refers to the amount of digits you wish to display.

Please reply in this topic if this reply did not help you.