dflow Posted March 15, 2011 Share Posted March 15, 2011 i have a price i want to add 10%-15% to the base price and that the result will be rounded to 9 in the ones digit place. for example 85 +15%=97.7 $result=99 117+15%=134.55 $result = 139 played with round() and ceil() Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/ Share on other sites More sharing options...
jcbones Posted March 16, 2011 Share Posted March 16, 2011 Do you ever round down, or leave the price as is? As in: 100 + 10% = 110 or 119 Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1187982 Share on other sites More sharing options...
cyberRobot Posted March 16, 2011 Share Posted March 16, 2011 It's very crude, but would something like this work: <?php //INITIALIZE VARIABLES $price = 85; $percent = 15; $percentIncrease = $price * ($percent/100); //GET NEW PRICE $newPrice = $price + $percentIncrease; //REMOVE DECIMAL $newPrice = (int)$newPrice; //you may want to use $newPrice = round($newPrice); //REPLACE LAST NUMBER WITH A NINE (and display results before and after) echo "<div>New price <b>before</b> rounding: $newPrice</div>"; $newPrice = substr($newPrice, 0, -1) . 9; echo "<div>New price <b>after</b> rounding: $newPrice</div>"; ?> Of course this won't work if the final price needs to be round down. It also doens't work if the $newPrice is 109.99 for example, but this could be fixed by using the round() or ceil() function. Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188009 Share on other sites More sharing options...
facarroll Posted March 16, 2011 Share Posted March 16, 2011 try ceil() and floor(). http://php.net/manual/en/function.round.php Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188018 Share on other sites More sharing options...
dflow Posted March 16, 2011 Author Share Posted March 16, 2011 Do you ever round down, or leave the price as is? As in: 100 + 10% = 110 or 119 this could be an option actually , but then ill need to isolate the ones digit, how would i do that? Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188256 Share on other sites More sharing options...
Mahngiel Posted March 16, 2011 Share Posted March 16, 2011 I never understood the value of pricing something as 99 or .99. But maybe it's just because I hate pennies. Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188320 Share on other sites More sharing options...
dflow Posted March 16, 2011 Author Share Posted March 16, 2011 I never understood the value of pricing something as 99 or .99. But maybe it's just because I hate pennies. it's psychological cognitive perception of price, it actually works Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188345 Share on other sites More sharing options...
Mahngiel Posted March 16, 2011 Share Posted March 16, 2011 it's psychological cognitive perception of price, it actually works Agreed that it works. My point is that personally, even as a ladd, I always shook my head at the practice. It wasn't until I studied law that I realized that not all of mankind is intelligent. Anyways, how's the progress? Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188349 Share on other sites More sharing options...
cyberRobot Posted March 16, 2011 Share Posted March 16, 2011 this could be an option actually , but then ill need to isolate the ones digit, how would i do that? You could utilize the code I posted earlier: substr($newPrice, 0, -1) Of course, you'll need to remove the decimal part first: $newPrice = (int)$newPrice; $newPrice = round($newPrice); Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188351 Share on other sites More sharing options...
dflow Posted March 16, 2011 Author Share Posted March 16, 2011 It's very crude, but would something like this work: <?php //INITIALIZE VARIABLES $price = 85; $percent = 15; $percentIncrease = $price * ($percent/100); //GET NEW PRICE $newPrice = $price + $percentIncrease; //REMOVE DECIMAL $newPrice = (int)$newPrice; //you may want to use $newPrice = round($newPrice); //REPLACE LAST NUMBER WITH A NINE (and display results before and after) echo "<div>New price <b>before</b> rounding: $newPrice</div>"; $newPrice = substr($newPrice, 0, -1) . 9; echo "<div>New price <b>after</b> rounding: $newPrice</div>"; ?> Of course this won't work if the final price needs to be round down. It also doens't work if the $newPrice is 109.99 for example, but this could be fixed by using the round() or ceil() function. gr8 thanks Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188415 Share on other sites More sharing options...
nicholasolsen Posted March 16, 2011 Share Posted March 16, 2011 This would do the trick in most cases i think: In this example the price is set to 7.51 $price = 7.51; $new_price = explode(".",$price); if ($new_price[1] > 50) { $cent=99; } else if ($new_price[1] < 50) { $cent=50; } $ending_price = "$new_price[0].$cent"; In this example the ending price would be 7.99 Quote Link to comment https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.