Jump to content

Archived

This topic is now archived and is closed to further replies.

DBookatay

Working with numbers and percents

Recommended Posts

Someone help, before I pull the rest of my hair out of my head...

I am attempting to create an accounting DB and need help with a line of code.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$balance = ($_POST['soldAmt']) + ($_POST['soldFees']) * ($_POST['rate']) - ($_POST['down']); [/quote]

Basically what Im trying to say is the "balance = soldAmt + soldFees x rate (which is %) - down. I'm having trouble turning ($_POST['rate']) into a percentage, and then x (multiply) it by ($_POST['soldAmt']) + ($_POST['soldFees']) - ($_POST['down'])

Any help?

Share this post


Link to post
Share on other sites
That's basic math... But do you want to

1. ADD the percentage: $value * (1 + $rate/100);
2. SUBTRACT the percentage: $value * (1 - $rate/100)
3. or simply GET the adjusted value: $value * $rate/100

Share this post


Link to post
Share on other sites
[!--quoteo(post=382091:date=Jun 9 2006, 07:05 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 9 2006, 07:05 PM) [snapback]382091[/snapback][/div][div class=\'quotemain\'][!--quotec--]
That's basic math... But do you want to

1. ADD the percentage: $value * (1 + $rate/100);
2. SUBTRACT the percentage: $value * (1 - $rate/100)
3. or simply GET the adjusted value: $value * $rate/100
[/quote]



"soldAmt" + "soldFees" + "rate" = _______ - "down"

Share this post


Link to post
Share on other sites
I still don't understand, but I'll assume:

[code]<?php

$balance = ($_POST['soldAmt'] + $_POST['soldFees']) * (1 + $_POST['rate']/100) - $_POST['down'];
echo 'Balance: ' . $balance;

?>

<form action="" method="post">
soldAmt: <input type="text" name="soldAmt" value="<?=$_POST['soldAmt']?>"><br />
soldFees: <input type="text" name="soldFees" value="<?=$_POST['soldFees']?>"><br />
rate: <input type="text" name="rate" value="<?=$_POST['rate']?>"><br />
down: <input type="text" name="down" value="<?=$_POST['down']?>"><br />
<input type="submit">
</form>[/code]

For soldAmt = 200, soldFees = 100, rate = 5, down = 40 it gives you balance = 275

[b]EDIT:[/b] 666th post...

Share this post


Link to post
Share on other sites
[!--quoteo(post=382103:date=Jun 9 2006, 07:36 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 9 2006, 07:36 PM) [snapback]382103[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[b]EDIT:[/b] 666th post...
[/quote]

Thanks for the help, Satan!


One more quick question, my "balance" row is "float" and if I insert $30.70 it inserts it into the db as 30.7.
How do I change this:
[code]$balance = '$ ' . $row['balance'];[/code] so that when it displays it will show again as $30.70.
Or if it's an even $30, how do I make it $30.00?

Share this post


Link to post
Share on other sites
Use the [a href=\"http://www.php.net/number_format\" target=\"_blank\"]number_format()[/a] function when displaying your numbers.

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=382107:date=Jun 9 2006, 08:00 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 9 2006, 08:00 PM) [snapback]382107[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Use the [a href=\"http://www.php.net/number_format\" target=\"_blank\"]number_format()[/a] function when displaying your numbers.

Ken
[/quote]

I tried that:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$balance = '$ ' . number_format($row['balance']);[/quote]

but then it takes off the cents leaving me with only a whole dollar ammount...

Share this post


Link to post
Share on other sites
The second parameter to the number_format() function is the number of decimal places. Try:
[code]<?php $balance = '$ ' . number_format($row['balance'],2); ?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.