SV: Round to a given value?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> > > On Wed, 2008-12-24 at 20:38 +0100, Anders Norrbring wrote:
> > > > Rounding an integer to the closest divisor by ten is easy with
> > > round() and a
> > > > negative precision, but I turn out to be lost if I want to round
> to
> > a
> > > given
> > > > number..
> > > >
> > > > Example, round to the closest 5000, or closest 400? Any great
> > ideas?
> > > >
> > > > Thanks, 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 the
> > PHP
> > > manual pages back in May. The manual pages are always useful, even
> if
> > > only for the user-written functions!
> > >
> > >
> > > Ash
> > > www.ashleysheridan.co.uk
> >
> >
> > Thanks 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 small
> in
> > the
> > 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!


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.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux