Best way to figure out a percent markup.

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
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Best way to figure out a percent markup.

Post by Red Blaze »

Ok, I gotta know what's the best way to figure out a mathematical function. I thought I figured it out, but it comes out different all the time. Here's what I did.

Code: Select all

$unitprice = '5.00';
$markup = 20;
$markup = $unitprice * ".$markup";
$unitprice = $unitprice + $markup;
echo $unitprice;
$markup is 20, for 20%. Which is why I added the period in front in line 3 for the 2nd $markup. I wanted to increase 5.00 by 20%, is what I'm trying to say. I dunno if that's the correct way, or if there's a better way. Help is well appreciated, thank you!

~RB
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Just multiply the unit price by 1.2 to add 20% on
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Code: Select all

$unitprice = '5.00';
$markup = 20;
$marked_up_price = (float)$unitprice * (($markup + 100)/100);

echo $marked_up_price
Do you have to declare $unitprice as a string? Take off the quotes and you won't have to convert it back to a float.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply