Form Calculater

Help!!! i am making a form that calculates this is the line that i am having problems with.
It works but when i add *.01+ the calculation is wrong. you can see the test page at
derolds.com/calc15.php
[php] $sum = floatval( $_POST[“indexEditbox1”] ) + floatval( $_POST[“indexEditbox2”] )
*.01+ floatval( $_POST[“calc15Editbox3”] ) + floatval( $_POST[“calc15Editbox4”] ) ;[/php]

You need to look into operator precedence: http://php.net/manual/en/language.operators.precedence.php

On your page you have: 100+100 *.01 +100+100=402

But this isn’t correct, this is how PHP interprets it:

100+
(100 * 0.01) = 1
= 101
+100
= 101
+100
= 301

If you want the original two values (2*100) plus 0.1 times this value you need to make the calculation happen in the right order, you also need to multiply by 1.01 not 0.01 because 200 * 0.01 is 2, 200 * 1.01 is 202.

100 +
100
= 200
* 1.01
= 202
+100
= 102
+100
= 202

So you want to use brackets to make the addition happen before the multiplication:

$sum = (100+100)*1.01 + 100 +100

So, try this:

[php]$sum = (floatval($_POST[“indexEditbox1”]) + floatval($_POST[“indexEditbox2”])) * 1.01+ floatval($_POST[“calc15Editbox3”]) + floatval($_POST[“calc15Editbox4”]);[/php]

Thanks Xerxes. That worked!!! ;D And now i under stand what was happing. Thank You

Ok so how would i do *3.5

Never mind I got it. That was a daa moment

Sponsor our Newsletter | Privacy Policy | Terms of Service