ducany ducany - 1 month ago 8
PHP Question

PHP Multiply currency variable with decimal

I'm trying to multiply a variable by a decimal (percentage) but it's outputting 0 as the value. The variable has a dollar sign and two decimal places ($53,900.00).

$baseprice = $53,900.00;
$ott = .92;
echo ($baseprice * $ott);


I think the currency sign ($) is messing it up. Can someone help?

Answer Source

You can also use the money_format() function to print it out with the dollar sign and comma. But when you declare a variable as a number it can't contain commas ',' or '$' or it gets cast to a string. Here is an example of using money_format.

$baseprice = 53900.00;
$ott = .92;
echo money_format('%n',($baseprice * $ott)); # prints $49,588.00