DBookatay Posted June 9, 2006 Share Posted June 9, 2006 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? Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/ Share on other sites More sharing options...
poirot Posted June 9, 2006 Share Posted June 9, 2006 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 Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/#findComment-43874 Share on other sites More sharing options...
DBookatay Posted June 9, 2006 Author Share Posted June 9, 2006 [!--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" Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/#findComment-43876 Share on other sites More sharing options...
poirot Posted June 9, 2006 Share Posted June 9, 2006 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... Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/#findComment-43886 Share on other sites More sharing options...
DBookatay Posted June 9, 2006 Author Share Posted June 9, 2006 [!--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? Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/#findComment-43888 Share on other sites More sharing options...
kenrbnsn Posted June 10, 2006 Share Posted June 10, 2006 Use the [a href=\"http://www.php.net/number_format\" target=\"_blank\"]number_format()[/a] function when displaying your numbers.Ken Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/#findComment-43890 Share on other sites More sharing options...
DBookatay Posted June 10, 2006 Author Share Posted June 10, 2006 [!--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... Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/#findComment-43919 Share on other sites More sharing options...
kenrbnsn Posted June 10, 2006 Share Posted June 10, 2006 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 Link to comment https://forums.phpfreaks.com/topic/11621-working-with-numbers-and-percents/#findComment-43920 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.