Search code examples
c#mathrounding

Round up values of text box in C#


I want to Roun d Up Values Like This...

0.1 will be 0

0.3 will be 0

0.5 will be 1

0.9 will be 1

Is there any way to solve it in c#?

Thanks in Advance...


Solution

  • You are looking for Math.Round you can read more about Round

    If the fractional component of number is halfway between two integers, one of which is even and the other odd, the even number is returned. Note that this method returns a Decimal instead of an integral type.

    and as suggested by @Micky you should be using MidpointRounding.AwayFromZero like

    Math.Round(0.5d, MidpointRounding.AwayFromZero));
    

    to get the correct output