Round to next value

R

RP

I want to ROUND the value if it is .5 or more to next integer. I am
using like this:

adAmount = Math.Round((basicPay * PerOrAmount) / 100, 0);

But it is not doing correctly. Suppose of adAmount = 132.5, it will
not do 133. If it exceeds .5 then it does. How to round when .5?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

RP said:
I want to ROUND the value if it is .5 or more to next integer. I am
using like this:

adAmount = Math.Round((basicPay * PerOrAmount) / 100, 0);

But it is not doing correctly. Suppose of adAmount = 132.5, it will
not do 133. If it exceeds .5 then it does. How to round when .5?

There are two possible causes:
- bankers rounding
- floating point

Try:

adAmount = Math.Fllor((basicPay * PerOrAmount) / 100 + 0.5);

Arne
 
J

Jose Fco Bonnin

You only needed to look the overloads of the method Math.Round, there is one
where you can specify the behavior when the number is just in the middle.

Specify the value MidpointRounding.AwayFromZero to round it to 133 or
MidpointRounding.ToEven to round it to 132.

Jose Fco Bonnin
 
M

Morten Wennevik [C# MVP]

I want to ROUND the value if it is .5 or more to next integer. I am
using like this:

adAmount = Math.Round((basicPay * PerOrAmount) / 100, 0);

But it is not doing correctly. Suppose of adAmount = 132.5, it will
not do 133. If it exceeds .5 then it does. How to round when .5?

Hi RP,

Use the overloaded Math.Round specifying MidpointRounding.AwayFromZero
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Arne said:
There are two possible causes:
- bankers rounding
- floating point

Try:

adAmount = Math.Fllor((basicPay * PerOrAmount) / 100 + 0.5);

Floor

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top