Home All Groups Group Topic Archive Search About

how to round number to custom step (0.25, 20, 100...)

Author
24 Oct 2006 3:55 AM
ibiza
Hi all,

I'd need a function that would rounds values like :
d = roundTo(64.2, 0.25) ' returns 64.25
d = roundTo(64.2, 10) ' returns 64
d = roundTo(64.2, 100) ' returns 100

well, that rounds a value to the given step...with rounding up if it is
it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)

can anyone help with this one?

Thanks! :)

Author
24 Oct 2006 6:03 AM
vincent
Try this (in C#).  It has not been tested.  It might not be the best but I
think it will work.


        public double roundTo(double numToRound, double step)
        {

            if (step == 0)
                return numToRound;

            //Calc the floor value of numToRound
            double floor = ((long) (numToRound / step)) * step;

            //round up if more than half way of step
            double round = floor;
            double remainder = numToRound - floor;
            if (remainder > step / 2)
                round += step;

            return round;

        }


Show quoteHide quote
"ibiza" wrote:

> Hi all,
>
> I'd need a function that would rounds values like :
> d = roundTo(64.2, 0.25) ' returns 64.25
> d = roundTo(64.2, 10) ' returns 64
> d = roundTo(64.2, 100) ' returns 100
>
> well, that rounds a value to the given step...with rounding up if it is
> it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)
>
> can anyone help with this one?
>
> Thanks! :)
>
>
Author
24 Oct 2006 6:18 AM
vincent
I found a bug.  The comparision in the if statement should be >= instead of >
     if (remainder >= step / 2)
                 round += step;

Show quoteHide quote
"vincent" wrote:

> Try this (in C#).  It has not been tested.  It might not be the best but I
> think it will work.
>
>
>         public double roundTo(double numToRound, double step)
>         {
>
>             if (step == 0)
>                 return numToRound;
>
>             //Calc the floor value of numToRound
>             double floor = ((long) (numToRound / step)) * step;
>
>             //round up if more than half way of step
>             double round = floor;
>             double remainder = numToRound - floor;
>             if (remainder > step / 2)
>                 round += step;
>
>             return round;
>
>         }
>
>
> "ibiza" wrote:
>
> > Hi all,
> >
> > I'd need a function that would rounds values like :
> > d = roundTo(64.2, 0.25) ' returns 64.25
> > d = roundTo(64.2, 10) ' returns 64
> > d = roundTo(64.2, 100) ' returns 100
> >
> > well, that rounds a value to the given step...with rounding up if it is
> > it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)
> >
> > can anyone help with this one?
> >
> > Thanks! :)
> >
> >
Author
24 Oct 2006 4:27 PM
ibiza
well, thank you very much! very appreciated ^_^
works fine!

vincent wrote:
Show quoteHide quote
> I found a bug.  The comparision in the if statement should be >= instead of >
>      if (remainder >= step / 2)
>                  round += step;
>
> "vincent" wrote:
>
> > Try this (in C#).  It has not been tested.  It might not be the best but I
> > think it will work.
> >
> >
> >         public double roundTo(double numToRound, double step)
> >         {
> >
> >             if (step == 0)
> >                 return numToRound;
> >
> >             //Calc the floor value of numToRound
> >             double floor = ((long) (numToRound / step)) * step;
> >
> >             //round up if more than half way of step
> >             double round = floor;
> >             double remainder = numToRound - floor;
> >             if (remainder > step / 2)
> >                 round += step;
> >
> >             return round;
> >
> >         }
> >
> >
> > "ibiza" wrote:
> >
> > > Hi all,
> > >
> > > I'd need a function that would rounds values like :
> > > d = roundTo(64.2, 0.25) ' returns 64.25
> > > d = roundTo(64.2, 10) ' returns 64
> > > d = roundTo(64.2, 100) ' returns 100
> > >
> > > well, that rounds a value to the given step...with rounding up if it is
> > > it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)
> > >
> > > can anyone help with this one?
> > >
> > > Thanks! :)
> > >
> > >
Author
24 Oct 2006 4:45 PM
Göran_Andersson
ibiza wrote:
Show quoteHide quote
> Hi all,
>
> I'd need a function that would rounds values like :
> d = roundTo(64.2, 0.25) ' returns 64.25
> d = roundTo(64.2, 10) ' returns 64
> d = roundTo(64.2, 100) ' returns 100
>
> well, that rounds a value to the given step...with rounding up if it is
> it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)
>
> can anyone help with this one?
>
> Thanks! :)
>

Simply:

Shared Function RoundTo(value As Double, step As Double) As Double
    Return Math.Round(value / step) * step
End Function

This will of course return the value 60 in the second case you listed,
not 64. I believe that's what you really intended.
Author
25 Oct 2006 12:11 AM
ibiza
wow, even simpler :)

and you got it right, the second case should have returned 60, my
mistake ;)

Göran Andersson wrote:
Show quoteHide quote
> ibiza wrote:
> > Hi all,
> >
> > I'd need a function that would rounds values like :
> > d = roundTo(64.2, 0.25) ' returns 64.25
> > d = roundTo(64.2, 10) ' returns 64
> > d = roundTo(64.2, 100) ' returns 100
> >
> > well, that rounds a value to the given step...with rounding up if it is
> > it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)
> >
> > can anyone help with this one?
> >
> > Thanks! :)
> >
>
> Simply:
>
> Shared Function RoundTo(value As Double, step As Double) As Double
>     Return Math.Round(value / step) * step
> End Function
>
> This will of course return the value 60 in the second case you listed,
> not 64. I believe that's what you really intended.