johnsmith153 Posted May 17, 2010 Share Posted May 17, 2010 I use: sprintf("%01.2f", $value); to display 3 as £3.00, and 1.7 as £1.70 etc. How would I round to the nearest 50p - so £1.70 = £1.50, £1.80 = £2.00 etc? Very simple if you know how! Quote Link to comment https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/ Share on other sites More sharing options...
MadTechie Posted May 17, 2010 Share Posted May 17, 2010 <?php function ceiling($number, $significance = 1) { return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false; } echo ceiling(1.3, 0.5); // 1.5 echo ceiling(1.7, 0.5); // 2 Use your own formatting to format it Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059586 Share on other sites More sharing options...
johnsmith153 Posted May 17, 2010 Author Share Posted May 17, 2010 Great. Thanks. However, I need to round it to the nearest. So 1.7 would be 1.5 and 1.2 would be 1 - the script shown would round up to 2/1.5. Any chance of showing where I would change the script? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059592 Share on other sites More sharing options...
MadTechie Posted May 17, 2010 Share Posted May 17, 2010 Sure function flooring($number, $significance = 1) { return ( is_numeric($number) && is_numeric($significance) ) ? (floor($number/$significance)*$significance) : false; } echo flooring(1.3, 0.5); // 1 echo flooring(1.7, 0.5); // 1.5 put in a simple inline code it would be $value = 1.7; echo sprintf("%01.2f", floor($value/0.5)*0.5); Quote Link to comment https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059596 Share on other sites More sharing options...
johnsmith153 Posted May 17, 2010 Author Share Posted May 17, 2010 Again, sorry this isn't right. The first example rounds everything up, the second example rounds everything down. I need it to round to the 'nearest'. So 1.7 would be closer to 1.5 than 2, so would return 1.5. 1.8 would be closer to 2 than 1.5, so would return 2. Quote Link to comment https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059600 Share on other sites More sharing options...
Psycho Posted May 17, 2010 Share Posted May 17, 2010 I think this is what you are after. It will round to the nearest value according to the $roundTo value passed function roundToPartial($value, $roundTo) { return round($value / $roundTo) * $roundTo; } echo roundToPartial(1.70, .5); //1.5 echo roundToPartial(1.80, .5); //2.0 Quote Link to comment https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059603 Share on other sites More sharing options...
johnsmith153 Posted May 17, 2010 Author Share Posted May 17, 2010 Perfect. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059607 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.