"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, February 20, 2011

Rounding Number in C#,[System.Math.Floor , System.Math.Ceiling, System.Math.Round]

 Hi,
i know this is an old topic but still some people searching and frequent question in Forums also.Anyway lets moves forward.
We can approach this requirement by 3 way, depending upon need because all are giving different result. :)
        1.System.Math.Floor 
        2.System.Math.Ceiling
        3.System.Math.Round
 
1.System.Math.Floor
System.Console.WriteLine("Floor "+ System.Math.Floor(2.1));
System.Console.WriteLine("Floor " System.Math.Floor(2.5));

Answers:
Floor 2
Floor 2
 
2.System.Math.Ceiling
System.Console.WriteLine("Ceiling " + System.Math.Ceiling(2.1));
System.Console.WriteLine("Ceiling " + System.Math.Ceiling(2.5));

Answer:
Ceiling 3
Ceiling 3
3.System.Math.Round
System.Console.WriteLine("Round " + System.Math.Round(1.1));
System.Console.WriteLine("Round " + System.Math.Round(2.5));

Answer:
Round 1
Round 3
 
 

Have a nice day... 'N happy Coding :)

No comments: