You are here: Re: multiplication math not working ? « PHP Programming Language « IT news, forums, messages
Re: multiplication math not working ?

Posted by One on 02/07/07 16:43

> 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 = 0.
>
> You might want to remove the currency symbol from the string, and just add
> it back when you have to echo/print the total.
>
> Also, I'd think if $number_percent = 25, you might want $total =
> $history['order_total'] * (1+($number_percent/100));
Hi Rik - thank you the reply.

I used string replace to remove the dollar sign, then I simply
multiplied the two numbers, and it is still calculating as zero.
Can I cast this variable to an int ??

Input :
$number_percent="25";
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * (1+($number_percent/100)));
echo "<br />total is ".$total."<br />";

Output :
history['order_total'] is $241.47
strip the $ sign 241.47
total is 0


:-(

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация