|
Posted by Rik on 02/07/07 16:06
One <david.hunter@gmail.com> wrote:
> hi group -
>
> I had this in a function - so I've just moved this out of the function=
> for readabilty.
>
> Can someone please tell me WHY does this not return the result of :
> n1 times n2
>
> As I said - I took it out of the function just for readability :
> In my php file :
>
> $number_percent=3D"25";
> echo "history['order_total'] is ".$history['order_total']."<br />";
> echo "number_percent is ".$number_percent."<br />";
> $total=3D(($history['order_total'])*$number_percent);
> echo "total is ".$total."<br />";
>
> And here is the output in the browser :
> history['order_total'] is $241.47
> number_percent is 25
> total is 0
>
> WHY ? Any advice or pointers would be much appreciated!
Because '$241.47' is not a number, it's a string representation of a =
currency. PHP doesn't know anything about that, 'currency' is not a type=
.. =
When performing math on it, PHP will try to make a number out of it, and=
=
encounters '$' as the first character. This is not something it =
recognizes, and it will default to 0. And 0 * 25 =3D 0.
You might want to remove the currency symbol from the string, and just a=
dd =
it back when you have to echo/print the total.
Also, I'd think if $number_percent =3D 25, you might want $total =3D =
$history['order_total'] * (1+($number_percent/100));
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|