Rounding numbers in C#




The code below shows how to round number using c# code

Floor will take a double and then convert it to the smallest whole number


System.Console.WriteLine( System.Math.Floor(1.1));
System.Console.WriteLine( System.Math.Floor(1.5));

Answers:
1
1

If you want the largest possible whole number, then you would want Ceiling

System.Console.WriteLine("Ceiling " + System.Math.Ceiling(1.1));
System.Console.WriteLine("Ceiling " + System.Math.Ceiling(1.5));

Answer:
2
2

If you want to round a number to the nearest decimal

System.Console.WriteLine("Round " + System.Math.Round(1.1));
System.Console.WriteLine("Round " + System.Math.Round(1.5));

Answer:
1
2


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: