Price + 20% of price in PHP [closed]









up vote
-2
down vote

favorite












PHP newbie here. I've created a function that increases prices based on their price range:



function update_price ( $price) 
If (isset($price) )
if ($price < 60 )
return $price + 40;

elseif ($price >= 60 && $price < 100)
return $price + 60;

elseif ($price >= 100 && $price < 200)
return $price + 70;

elseif ($price >= 200 && $price < 300)
return $price + 80;

elseif ($price >= 300 && $price < 450)
return $price + 90;

elseif ($price >= 450)
return $price + ($price * .2);





The function is called when I import a CSV file. Works great, except for the last part of the function. I'm trying to raise the price of those qualifying products by 20%, so I'm taking $price and adding ($price * .2). But a product that was $628.74 is coming out as $754,888.00 instead of $754.88.



What did I do wrong?










share|improve this question















closed as off-topic by Funk Forty Niner, Paul Crovella, Davіd, user6655984, Nick Nov 12 at 0:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Funk Forty Niner, Paul Crovella, Davіd, Community
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Lots of typographical errors in the code you are showing (missing several right parentheses). Hard to tell what's really going on since who knows what else may be incorrectly typed. Why don't you just copy/paste the actual code you want to ask about into the question rather than retyping it?
    – lurker
    Nov 11 at 23:30










  • You've also got a typo in your expected result. $628.74 * 1.2 should come out as $754.488.
    – jmbpiano
    Nov 11 at 23:31






  • 1




    lurker - You're right. I've corrected the parentheses. Like I said, total newbie here and I copied/pasted the wrong version.
    – Bill Horvath
    Nov 11 at 23:58










  • Your function works fine. The problem must be in how you are outputting the result. 3v4l.org/nAgcT
    – Nick
    Nov 12 at 0:36














up vote
-2
down vote

favorite












PHP newbie here. I've created a function that increases prices based on their price range:



function update_price ( $price) 
If (isset($price) )
if ($price < 60 )
return $price + 40;

elseif ($price >= 60 && $price < 100)
return $price + 60;

elseif ($price >= 100 && $price < 200)
return $price + 70;

elseif ($price >= 200 && $price < 300)
return $price + 80;

elseif ($price >= 300 && $price < 450)
return $price + 90;

elseif ($price >= 450)
return $price + ($price * .2);





The function is called when I import a CSV file. Works great, except for the last part of the function. I'm trying to raise the price of those qualifying products by 20%, so I'm taking $price and adding ($price * .2). But a product that was $628.74 is coming out as $754,888.00 instead of $754.88.



What did I do wrong?










share|improve this question















closed as off-topic by Funk Forty Niner, Paul Crovella, Davіd, user6655984, Nick Nov 12 at 0:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Funk Forty Niner, Paul Crovella, Davіd, Community
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Lots of typographical errors in the code you are showing (missing several right parentheses). Hard to tell what's really going on since who knows what else may be incorrectly typed. Why don't you just copy/paste the actual code you want to ask about into the question rather than retyping it?
    – lurker
    Nov 11 at 23:30










  • You've also got a typo in your expected result. $628.74 * 1.2 should come out as $754.488.
    – jmbpiano
    Nov 11 at 23:31






  • 1




    lurker - You're right. I've corrected the parentheses. Like I said, total newbie here and I copied/pasted the wrong version.
    – Bill Horvath
    Nov 11 at 23:58










  • Your function works fine. The problem must be in how you are outputting the result. 3v4l.org/nAgcT
    – Nick
    Nov 12 at 0:36












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











PHP newbie here. I've created a function that increases prices based on their price range:



function update_price ( $price) 
If (isset($price) )
if ($price < 60 )
return $price + 40;

elseif ($price >= 60 && $price < 100)
return $price + 60;

elseif ($price >= 100 && $price < 200)
return $price + 70;

elseif ($price >= 200 && $price < 300)
return $price + 80;

elseif ($price >= 300 && $price < 450)
return $price + 90;

elseif ($price >= 450)
return $price + ($price * .2);





The function is called when I import a CSV file. Works great, except for the last part of the function. I'm trying to raise the price of those qualifying products by 20%, so I'm taking $price and adding ($price * .2). But a product that was $628.74 is coming out as $754,888.00 instead of $754.88.



What did I do wrong?










share|improve this question















PHP newbie here. I've created a function that increases prices based on their price range:



function update_price ( $price) 
If (isset($price) )
if ($price < 60 )
return $price + 40;

elseif ($price >= 60 && $price < 100)
return $price + 60;

elseif ($price >= 100 && $price < 200)
return $price + 70;

elseif ($price >= 200 && $price < 300)
return $price + 80;

elseif ($price >= 300 && $price < 450)
return $price + 90;

elseif ($price >= 450)
return $price + ($price * .2);





The function is called when I import a CSV file. Works great, except for the last part of the function. I'm trying to raise the price of those qualifying products by 20%, so I'm taking $price and adding ($price * .2). But a product that was $628.74 is coming out as $754,888.00 instead of $754.88.



What did I do wrong?







php percentage multiplying






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 23:57









Javier Larroulet

1,258315




1,258315










asked Nov 11 at 23:25









Bill Horvath

82




82




closed as off-topic by Funk Forty Niner, Paul Crovella, Davіd, user6655984, Nick Nov 12 at 0:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Funk Forty Niner, Paul Crovella, Davіd, Community
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Funk Forty Niner, Paul Crovella, Davіd, user6655984, Nick Nov 12 at 0:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Funk Forty Niner, Paul Crovella, Davіd, Community
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 1




    Lots of typographical errors in the code you are showing (missing several right parentheses). Hard to tell what's really going on since who knows what else may be incorrectly typed. Why don't you just copy/paste the actual code you want to ask about into the question rather than retyping it?
    – lurker
    Nov 11 at 23:30










  • You've also got a typo in your expected result. $628.74 * 1.2 should come out as $754.488.
    – jmbpiano
    Nov 11 at 23:31






  • 1




    lurker - You're right. I've corrected the parentheses. Like I said, total newbie here and I copied/pasted the wrong version.
    – Bill Horvath
    Nov 11 at 23:58










  • Your function works fine. The problem must be in how you are outputting the result. 3v4l.org/nAgcT
    – Nick
    Nov 12 at 0:36












  • 1




    Lots of typographical errors in the code you are showing (missing several right parentheses). Hard to tell what's really going on since who knows what else may be incorrectly typed. Why don't you just copy/paste the actual code you want to ask about into the question rather than retyping it?
    – lurker
    Nov 11 at 23:30










  • You've also got a typo in your expected result. $628.74 * 1.2 should come out as $754.488.
    – jmbpiano
    Nov 11 at 23:31






  • 1




    lurker - You're right. I've corrected the parentheses. Like I said, total newbie here and I copied/pasted the wrong version.
    – Bill Horvath
    Nov 11 at 23:58










  • Your function works fine. The problem must be in how you are outputting the result. 3v4l.org/nAgcT
    – Nick
    Nov 12 at 0:36







1




1




Lots of typographical errors in the code you are showing (missing several right parentheses). Hard to tell what's really going on since who knows what else may be incorrectly typed. Why don't you just copy/paste the actual code you want to ask about into the question rather than retyping it?
– lurker
Nov 11 at 23:30




Lots of typographical errors in the code you are showing (missing several right parentheses). Hard to tell what's really going on since who knows what else may be incorrectly typed. Why don't you just copy/paste the actual code you want to ask about into the question rather than retyping it?
– lurker
Nov 11 at 23:30












You've also got a typo in your expected result. $628.74 * 1.2 should come out as $754.488.
– jmbpiano
Nov 11 at 23:31




You've also got a typo in your expected result. $628.74 * 1.2 should come out as $754.488.
– jmbpiano
Nov 11 at 23:31




1




1




lurker - You're right. I've corrected the parentheses. Like I said, total newbie here and I copied/pasted the wrong version.
– Bill Horvath
Nov 11 at 23:58




lurker - You're right. I've corrected the parentheses. Like I said, total newbie here and I copied/pasted the wrong version.
– Bill Horvath
Nov 11 at 23:58












Your function works fine. The problem must be in how you are outputting the result. 3v4l.org/nAgcT
– Nick
Nov 12 at 0:36




Your function works fine. The problem must be in how you are outputting the result. 3v4l.org/nAgcT
– Nick
Nov 12 at 0:36












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










It's OK that PHP returns 754.488, it's the same value returned by a calculator:
628.74÷5+628.74=754.488



Here you would need to round the precision to 2 decimal numbers, this can be done with the round() function.



Also, I would write your function like this:



 function update_price($price) (!is_numeric($price)) 





share|improve this answer




















  • Can you tell me what "$price/5, 2, PHP_ROUND_HALF_DOWN" does?
    – Bill Horvath
    Nov 12 at 0:07










  • $price/5 is like doing $price/100*20 , the round() function is the one that will round your number to 2 decimal numbers. Please check php.net/manual/en/function.round.php
    – dAm2K
    Nov 12 at 0:12










  • That worked perfectly. Thanks!
    – Bill Horvath
    Nov 13 at 0:09

















up vote
0
down vote













im not sure what your doing to give you that error but



Testcode



$base = 628.74;
$change = round($base * 0.2,2);
$new = $base + $change;
$allatonce = round(($base+($base*0.2)),2);
print_r(array($base,$change,$new,$allatonce));


Output




Array ( [0] => 628.74 [1] => 125.75 [2] => 754.49 [3] => 754.49 )




the all at once value would fit your needs.






share|improve this answer
















  • 1




    Can you explain how this is different to the OP's original solution?
    – Davіd
    Nov 11 at 23:41










  • its not missing the ) after ($price >= 450 it also rounds to 2 dp.
    – IcePops
    Nov 11 at 23:46


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










It's OK that PHP returns 754.488, it's the same value returned by a calculator:
628.74÷5+628.74=754.488



Here you would need to round the precision to 2 decimal numbers, this can be done with the round() function.



Also, I would write your function like this:



 function update_price($price) (!is_numeric($price)) 





share|improve this answer




















  • Can you tell me what "$price/5, 2, PHP_ROUND_HALF_DOWN" does?
    – Bill Horvath
    Nov 12 at 0:07










  • $price/5 is like doing $price/100*20 , the round() function is the one that will round your number to 2 decimal numbers. Please check php.net/manual/en/function.round.php
    – dAm2K
    Nov 12 at 0:12










  • That worked perfectly. Thanks!
    – Bill Horvath
    Nov 13 at 0:09














up vote
0
down vote



accepted










It's OK that PHP returns 754.488, it's the same value returned by a calculator:
628.74÷5+628.74=754.488



Here you would need to round the precision to 2 decimal numbers, this can be done with the round() function.



Also, I would write your function like this:



 function update_price($price) (!is_numeric($price)) 





share|improve this answer




















  • Can you tell me what "$price/5, 2, PHP_ROUND_HALF_DOWN" does?
    – Bill Horvath
    Nov 12 at 0:07










  • $price/5 is like doing $price/100*20 , the round() function is the one that will round your number to 2 decimal numbers. Please check php.net/manual/en/function.round.php
    – dAm2K
    Nov 12 at 0:12










  • That worked perfectly. Thanks!
    – Bill Horvath
    Nov 13 at 0:09












up vote
0
down vote



accepted







up vote
0
down vote



accepted






It's OK that PHP returns 754.488, it's the same value returned by a calculator:
628.74÷5+628.74=754.488



Here you would need to round the precision to 2 decimal numbers, this can be done with the round() function.



Also, I would write your function like this:



 function update_price($price) (!is_numeric($price)) 





share|improve this answer












It's OK that PHP returns 754.488, it's the same value returned by a calculator:
628.74÷5+628.74=754.488



Here you would need to round the precision to 2 decimal numbers, this can be done with the round() function.



Also, I would write your function like this:



 function update_price($price) (!is_numeric($price)) 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 0:01









dAm2K

7,80843038




7,80843038











  • Can you tell me what "$price/5, 2, PHP_ROUND_HALF_DOWN" does?
    – Bill Horvath
    Nov 12 at 0:07










  • $price/5 is like doing $price/100*20 , the round() function is the one that will round your number to 2 decimal numbers. Please check php.net/manual/en/function.round.php
    – dAm2K
    Nov 12 at 0:12










  • That worked perfectly. Thanks!
    – Bill Horvath
    Nov 13 at 0:09
















  • Can you tell me what "$price/5, 2, PHP_ROUND_HALF_DOWN" does?
    – Bill Horvath
    Nov 12 at 0:07










  • $price/5 is like doing $price/100*20 , the round() function is the one that will round your number to 2 decimal numbers. Please check php.net/manual/en/function.round.php
    – dAm2K
    Nov 12 at 0:12










  • That worked perfectly. Thanks!
    – Bill Horvath
    Nov 13 at 0:09















Can you tell me what "$price/5, 2, PHP_ROUND_HALF_DOWN" does?
– Bill Horvath
Nov 12 at 0:07




Can you tell me what "$price/5, 2, PHP_ROUND_HALF_DOWN" does?
– Bill Horvath
Nov 12 at 0:07












$price/5 is like doing $price/100*20 , the round() function is the one that will round your number to 2 decimal numbers. Please check php.net/manual/en/function.round.php
– dAm2K
Nov 12 at 0:12




$price/5 is like doing $price/100*20 , the round() function is the one that will round your number to 2 decimal numbers. Please check php.net/manual/en/function.round.php
– dAm2K
Nov 12 at 0:12












That worked perfectly. Thanks!
– Bill Horvath
Nov 13 at 0:09




That worked perfectly. Thanks!
– Bill Horvath
Nov 13 at 0:09












up vote
0
down vote













im not sure what your doing to give you that error but



Testcode



$base = 628.74;
$change = round($base * 0.2,2);
$new = $base + $change;
$allatonce = round(($base+($base*0.2)),2);
print_r(array($base,$change,$new,$allatonce));


Output




Array ( [0] => 628.74 [1] => 125.75 [2] => 754.49 [3] => 754.49 )




the all at once value would fit your needs.






share|improve this answer
















  • 1




    Can you explain how this is different to the OP's original solution?
    – Davіd
    Nov 11 at 23:41










  • its not missing the ) after ($price >= 450 it also rounds to 2 dp.
    – IcePops
    Nov 11 at 23:46















up vote
0
down vote













im not sure what your doing to give you that error but



Testcode



$base = 628.74;
$change = round($base * 0.2,2);
$new = $base + $change;
$allatonce = round(($base+($base*0.2)),2);
print_r(array($base,$change,$new,$allatonce));


Output




Array ( [0] => 628.74 [1] => 125.75 [2] => 754.49 [3] => 754.49 )




the all at once value would fit your needs.






share|improve this answer
















  • 1




    Can you explain how this is different to the OP's original solution?
    – Davіd
    Nov 11 at 23:41










  • its not missing the ) after ($price >= 450 it also rounds to 2 dp.
    – IcePops
    Nov 11 at 23:46













up vote
0
down vote










up vote
0
down vote









im not sure what your doing to give you that error but



Testcode



$base = 628.74;
$change = round($base * 0.2,2);
$new = $base + $change;
$allatonce = round(($base+($base*0.2)),2);
print_r(array($base,$change,$new,$allatonce));


Output




Array ( [0] => 628.74 [1] => 125.75 [2] => 754.49 [3] => 754.49 )




the all at once value would fit your needs.






share|improve this answer












im not sure what your doing to give you that error but



Testcode



$base = 628.74;
$change = round($base * 0.2,2);
$new = $base + $change;
$allatonce = round(($base+($base*0.2)),2);
print_r(array($base,$change,$new,$allatonce));


Output




Array ( [0] => 628.74 [1] => 125.75 [2] => 754.49 [3] => 754.49 )




the all at once value would fit your needs.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 23:33









IcePops

64




64







  • 1




    Can you explain how this is different to the OP's original solution?
    – Davіd
    Nov 11 at 23:41










  • its not missing the ) after ($price >= 450 it also rounds to 2 dp.
    – IcePops
    Nov 11 at 23:46













  • 1




    Can you explain how this is different to the OP's original solution?
    – Davіd
    Nov 11 at 23:41










  • its not missing the ) after ($price >= 450 it also rounds to 2 dp.
    – IcePops
    Nov 11 at 23:46








1




1




Can you explain how this is different to the OP's original solution?
– Davіd
Nov 11 at 23:41




Can you explain how this is different to the OP's original solution?
– Davіd
Nov 11 at 23:41












its not missing the ) after ($price >= 450 it also rounds to 2 dp.
– IcePops
Nov 11 at 23:46





its not missing the ) after ($price >= 450 it also rounds to 2 dp.
– IcePops
Nov 11 at 23:46




這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto