Resources » Code Snippets » C# Syntax

Rounding numbers in C#


Posted Date: 03-Mar-2007  Last Updated:   Category: C# Syntax    
Author: Member Level: Gold    Points: 10






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


Did you like this resource? Share it with your friends and show your love!

Responses to "Rounding numbers in C# "

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

Feedbacks      

Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Roman Goryelov
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India