Jump to content


Photo

Working with numbers and percents


  • Please log in to reply
7 replies to this topic

#1 DBookatay

DBookatay
  • Members
  • PipPipPip
  • Advanced Member
  • 334 posts
  • LocationCT

Posted 09 June 2006 - 10:49 PM

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?

$result mysql_query("SELECT finger FROM hand WHERE id=3"); 
echo 
$result


#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 09 June 2006 - 11:05 PM

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
~ D Kuang

#3 DBookatay

DBookatay
  • Members
  • PipPipPip
  • Advanced Member
  • 334 posts
  • LocationCT

Posted 09 June 2006 - 11:09 PM

[!--quoteo(post=382091:date=Jun 9 2006, 07:05 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 9 2006, 07:05 PM) View Post[/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"

$result mysql_query("SELECT finger FROM hand WHERE id=3"); 
echo 
$result


#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 09 June 2006 - 11:36 PM

I still don't understand, but I'll assume:

<?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>

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

EDIT: 666th post...
~ D Kuang

#5 DBookatay

DBookatay
  • Members
  • PipPipPip
  • Advanced Member
  • 334 posts
  • LocationCT

Posted 09 June 2006 - 11:47 PM

[!--quoteo(post=382103:date=Jun 9 2006, 07:36 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 9 2006, 07:36 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
EDIT: 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:
$balance = '$ ' . $row['balance'];
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?

$result mysql_query("SELECT finger FROM hand WHERE id=3"); 
echo 
$result


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 June 2006 - 12:00 AM

Use the [a href=\"http://www.php.net/number_format\" target=\"_blank\"]number_format()[/a] function when displaying your numbers.

Ken

#7 DBookatay

DBookatay
  • Members
  • PipPipPip
  • Advanced Member
  • 334 posts
  • LocationCT

Posted 10 June 2006 - 03:49 AM

[!--quoteo(post=382107:date=Jun 9 2006, 08:00 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 9 2006, 08:00 PM) View Post[/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...

$result mysql_query("SELECT finger FROM hand WHERE id=3"); 
echo 
$result


#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 June 2006 - 04:11 AM

The second parameter to the number_format() function is the number of decimal places. Try:
<?php $balance = '$ ' . number_format($row['balance'],2); ?>

Ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users