how to convert string to number for calculations...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

how to convert string to number for calculations...

Post by grabber_grabbs »

I have $qty and $amount that are both strings. i would like to do $qty * $amount.
I have tried intval($qty) * intval(amount) but i loose the decimal if i do it that way... 4 * 8.29 = 32

1.75 * 2.00 = 3.50 < this is what i want.

thanks.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to convert string to number for calculations...

Post by Celauran »

grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

Re: how to convert string to number for calculations...

Post by grabber_grabbs »

Thanks Celauran again, do you know why this is not working ?

$extvalue = floatval($QUANTITY_0) * floatval($PRICE_0);
$extvalue = sprintf("%8,2", $extvalue);

echo $extvalue
// if qty = 1 and price = 1.50 i still have 1.5 after the springf function applied. I would like to have 1.50 for the result.
// floatval is doing what it should.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to convert string to number for calculations...

Post by Celauran »

Code: Select all

printf("%01.2f", $val);
Post Reply