graham23s Posted July 5, 2008 Share Posted July 5, 2008 Hi Guys, i don;t know if this is at all possible but say i have 2 prices in mysql: 1.99 and 2.99 is it possible to work out the % you have saved from the old to the new price? i.e you have saved 11% thanks for any info guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/ Share on other sites More sharing options...
MasterACE14 Posted July 5, 2008 Share Posted July 5, 2008 yes. like this: <?php $percent = ($low_value / $high_value) * 100; Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/#findComment-582193 Share on other sites More sharing options...
graham23s Posted July 5, 2008 Author Share Posted July 5, 2008 Hi Mate, thanks for that! i did this: <?php $low_value = 2.99; $high_value = 5.99; $percent = ($low_value / $high_value) * 100; $p = number_format($percent); print "You have saved $p%!"; ?> it gives me 50% saving is it ok to round it using number format do you think? cheers mate Graham Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/#findComment-582197 Share on other sites More sharing options...
MasterACE14 Posted July 5, 2008 Share Posted July 5, 2008 use round() to round up, or ceil() to round down. number_format() is for large numbers to put the comma in after every 3 digits. like 1,000,000 Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/#findComment-582201 Share on other sites More sharing options...
graham23s Posted July 5, 2008 Author Share Posted July 5, 2008 brill! thanks ace Graham Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/#findComment-582215 Share on other sites More sharing options...
MasterACE14 Posted July 5, 2008 Share Posted July 5, 2008 no problem mate, any time Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/#findComment-582225 Share on other sites More sharing options...
AndyB Posted July 5, 2008 Share Posted July 5, 2008 um, the saving is actually high value minus low value all divided by high value, not low value divided by high value Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/#findComment-582230 Share on other sites More sharing options...
.josh Posted July 5, 2008 Share Posted July 5, 2008 use round() to round up, or ceil() to round down. number_format() is for large numbers to put the comma in after every 3 digits. like 1,000,000 Regards ACE floor() rounds down. ceil() rounds up. round() rounds to nearest int. <?php for ($x = 1.1; $x < 2; $x += .1) { echo "[$x] floor " . floor($x) . " | ceil " . ceil($x) . " | round " . round($x) . "<br />"; } ?> output: [1.1] floor 1 | ceil 2 | round 1 [1.2] floor 1 | ceil 2 | round 1 [1.3] floor 1 | ceil 2 | round 1 [1.4] floor 1 | ceil 2 | round 1 [1.5] floor 1 | ceil 2 | round 2 [1.6] floor 1 | ceil 2 | round 2 [1.7] floor 1 | ceil 2 | round 2 [1.8] floor 1 | ceil 2 | round 2 [1.9] floor 1 | ceil 2 | round 2 Quote Link to comment https://forums.phpfreaks.com/topic/113319-working-out-percentages/#findComment-582247 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.