- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Math.Round() Method in C#
The Math.Round() method in C# rounds a value to the nearest integer or to the specified number of fractional digits.
Methods
The following are the methods overloaded by Math.Round() −
Math.Round(Double) Math.Round(Double, Int32) Math.Round(Double, Int32, MidpointRounding) Math.Round(Double, MidpointRounding) Math.Round(Decimal) Math.Round(Decimal, Int32) Math.Round(Decimal, Int32, MidpointRounding) Math.Round(Decimal, MidpointRounding)
Example
Let us now see an example to implement Math.Round() method i.e. Math.Round(Decimal) −
using System; public class Demo { public static void Main(){ Decimal val1 = 78.12m; Decimal val2 = 30.675m; Console.WriteLine("Decimal Value = " + val1); Console.WriteLine("Rounded value = " + Math.Round(val1)); Console.WriteLine("Decimal Value = " + val2); Console.WriteLine("Rounded value = " + Math.Round(val2)); } }
Output
This will produce the following output −
Decimal Value = 78.12 Rounded value = 78 Decimal Value = 30.675 Rounded value = 31
Example
Let us see another example to implement Math.Round() method i.e. Math.Round(Double) −
using System; public class Demo { public static void Main(){ Double val1 = 23.10; Double val2 = 90.98; Console.WriteLine("Double Value = " + val1); Console.WriteLine("Rounded value = " + Math.Round(val1)); Console.WriteLine("Double Value = " + val2); Console.WriteLine("Rounded value = " + Math.Round(val2)); } }
Output
This will produce the following output −
Double Value = 23.1 Rounded value = 23 Double Value = 90.98 Rounded value = 91
Advertisements
To Continue Learning Please Login
Login with Google