PositiveGuy
PositiveGuy

Reputation: 47763

Round up from .5 in a %

Ceiling rounds up no matter if you get .1, .3, .5, .7. or whatever the value is for the decimal.

I need to know how to only round up if you have .5. So for example [number].5 round up.

Anyone know how to do this?

Upvotes: 1

Views: 3041

Answers (3)

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7846

Math.Round(0.5) works as expected.

EDIT

Math.Round(0.5, MidpointRounding.AwayFromZero) rounds up if you're on 0.5

Upvotes: 11

Paul Ruane
Paul Ruane

Reputation: 38600

Math.Round(number, MidpointRounding.AwayFromZero);

Details and comparison with the default can be seen on the System.MidpointRounding page.

Upvotes: 5

dlev
dlev

Reputation: 48596

Math.Round() uses banker's rounding by default, so it will round to the nearest even number when dealing with [number].5. In other words, Math.Round(0.5) is 0, while Math.Round(1.5) is 2. You can make it always round up by saying Math.Round(0.5, MidpointRounding.AwayFromZero);

Upvotes: 6

Related Questions