Re: SV: [PHP] Round to a given value?
| From: | Jim Lucas | Date: | Fri, 26 Dec 2008 05:51:52 +0000 |
| Subject: | Re: SV: [PHP] Round to a given value? | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-285070@lists.php.net to get a copy of this message | ||
Anders Norrbring wrote:
Isn't what you wrote the same as the following? function Multiple($number, $multiple) {Dear Lord.. It seems like it was so much simpler than I would even guess.. Maybe that's why I didn't see it? function roundto($number, $to) { return round($number / $to) * $to; } Anders.toOn Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:Rounding an integer to the closest divisor by ten is easy withround() and anegative precision, but I turn out to be lost if I want to roundaifgivenideas?number.. Example, round to the closest 5000, or closest 400? Any greatPHPThanks, and merry x-mas! Anders.http://www.alixaxel.com/wordpress/wp-content/2008/05/Round.php Someone was way ahead of you and found the answer and posted on themanual pages back in May. The manual pages are always useful, eveninonly for the user-written functions! Ash www.ashleysheridan.co.ukThanks Ash. I browse the pages frequently for ideas, and also did it yesterday.. However, I must have missed that post, maybe because it was so smallthe list.. ;-) Thanks for the tip! Anders.I cheered too fast.. That function rounds to the closest 500, literally.. So, 1100 is rounded to 1500, not to 1000 which would be the correct value. Any more ideas from anyone? My mind isn't working right at the moment, so a "ready-to-plug-in" function would be really great!
return round($number / $multiple) * $multiple;} The above was on the page that Ash linked to... Named differently true, but it was right there in front of you...