Rounding up to .05

  • Thread starter Thread starter Raj
  • Start date Start date
How do I round up a number to the nearest 0.05?

What do you mean? Could you specify the rules you use to do that?

for example,

100.251 - 100.253 ~ 100.250
100.254 - 100.256 ~ 100.255
100.257 - 100.259 ~ 100.260

is it what you suppose to have?
 
.... but for answers there ...like 1+ Math.Floor(x) , and even a long
discussion about why (int)(x + 0.4) is better than (int) ( x+ 0.5) ,
with some sites where you have to "pay" to see the 'certified solution(s)'
!!



Math.Floor( 0.5 + 20*x) / 20.0

to round-to-the-nearest 1/20, not necessary up, and that, for positive
values of x. To round-up-to the next 1/20 (unless it is already an exact
1/20), rather use

Math.Ceiling( 20*x ) / 20.0


It may also be a technical problem about what it really does for x a
floating point number, since technically it makes sense for decimal number,
but for a float...?

Finally, it may not be in agreement with banks policy, which don't
systematically round someInt + 0.5 always up to the someInt+1 (they
would lose money, given the large amount of rounding operations they
perform).




Vanderghast, Access MVP
 
Here is an easy solution to wrap your head around:

public static double RoundToPointOhFive(double input)
{
return Math.Round(input*20)/20;
}

This gives you the following table:

123 = 123
123.01 = 123
123.02 = 123
123.03 = 123.05
123.04 = 123.05
123.05 = 123.05
123.06 = 123.05
123.07 = 123.05
123.08 = 123.1
123.09 = 123.1
123.1 = 123.1

You can break it up even farther if you want to test it more. How did I get
20 here?

1. I need to get to the item I want to round on. In this case, multiply by
10

1230.6

2. I am rounding now on .5, so I have to make it 1. This is done by
multiplying by 2.

2461.2

You then round

2461

And divide by 20

123.05

I assume this is what you mean when you say round to nearest .05? If so, you
can determine if there is a better way to refactor this. ;-)

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 
How do I round up a number to the nearest 0.05?

There are multiple ways of doing that.

One of them is:

public static decimal FlexRound(decimal x, decimal unit)
{
int n = (int)(1 / unit);
if(unit * n != 1.00m)
{
throw new ArgumentException("Invalid unit: " + unit);
}
return ((decimal)((int)(x * n + 0.50m))) / n;
}

Arne
 
Try out the solution with these numbers:
16.489 should return 16.49
6.66 should return 6.70
etc.,

None of the solution does this, i am sorry!
 
Raj said:
Try out the solution with these numbers:
16.489 should return 16.49

Why not 16.50? How is 16.49 a multiple of 0.05 at all?
6.66 should return 6.70
etc.,

None of the solution does this, i am sorry!

IMHO, your problem statement is poorly defined. But, assuming you mean
that you always want numbers not a multiple of 0.05 to be rounded
upwards to the next multiple of 0.05, what is wrong with the solution
provided here:
[...] To round-up-to the next 1/20 (unless it is already an exact
1/20), rather use

Math.Ceiling( 20*x ) / 20.0

Noting, of course, his caveat regarding the usefulness of such a
rounding for values of type float or double. That's why Arne's solution
uses the decimal type (though due to the ambiguous problem statement, it
appears he solved a problem different from the one you actually are
interested in).

Pete
 
None of the solution does this, i am sorry!
Try out the solution with these numbers:
16.489 should return 16.49
6.66 should return 6.70
etc.,

The question was:

#How do I round up a number to the nearest 0.05?

16.489 rounded to nearest 0.05 is 16.50 and 6.66 rounded
to nearest 0.05 is 6.65.

At least according to normal understanding of rounding
to nearest 0.05.

I very much hope that my code do return those results.

If you have a different meaning of rounding
to nearest 0.05, then please explain what it means.

Arne
 
Arne said:
The question was:

#How do I round up a number to the nearest 0.05?

True. Note the word "up" in the above sentence.
16.489 rounded to nearest 0.05 is 16.50 and 6.66 rounded
to nearest 0.05 is 6.65.

True. But, 6.66 rounded *UP* to the nearest 0.05 is 6.70.
At least according to normal understanding of rounding
to nearest 0.05.

I very much hope that my code do return those results. [...]

Your code works fine for the problem you were trying to solve. But I
believe you have misunderstood what the OP is asking for.

Pete
 
Arne said:
The question was:

#How do I round up a number to the nearest 0.05?

True. Note the word "up" in the above sentence.
16.489 rounded to nearest 0.05 is 16.50 and 6.66 rounded
to nearest 0.05 is 6.65.

True. But, 6.66 rounded *UP* to the nearest 0.05 is 6.70.
At least according to normal understanding of rounding
to nearest 0.05.

I very much hope that my code do return those results. [...]

Your code works fine for the problem you were trying to solve. But I
believe you have misunderstood what the OP is asking for.

Possible.

Normally one rounds either up or down or to nearest.

I let nearest take precedence over up in my interpretation
of the question.

If it is up then:

return ((decimal)((int)(x * n + 0.50m))) / n;

should be replaced by:

return Math.Ceiling(x * n) / n;

Arne
 
Back
Top