Convert a floating-point number to nearest int in C#
This post will discuss how to convert a floating-point number to the nearest int in C#.
1. Using Math.Round() method
The most common approach to round a value to the nearest integer is using the Math.Round() method. However, this method returns a Decimal instead of an integer, and you need to cast the result to an integer. The following example illustrates.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 5.51f; int i = (int)Math.Round(f); Console.WriteLine(i); // 6 } } |
The Math.Round() method uses the round to nearest even convention by default. It is overloaded to take the MidpointRounding Enum, which specifies the strategy used for rounding the specified number. For example, the following code uses the rounding convention of MidpointRounding.AwayFromZero.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 5.51f; int i = (int) Math.Round(f, MidpointRounding.AwayFromZero); Console.WriteLine(i); // 6 } } |
For more information on rounding numbers with midpoint values, see the official documentation of the Midpoint values and rounding conventions.
2. Using Math.Ceiling() method
An alternative to the Math.Round() method is to use Math.Ceiling() method. It returns the nearest integral value greater than or equal to the specified number. The following example shows the invocation for this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 5.51f; int i = (int)Math.Ceiling(f); Console.WriteLine(i); // 6 } } |
3. Using Convert.ToInt32() method
Both Math.Round() and Math.Ceiling() methods returns a Decimal instead of an integral value. You can also use the Convert.ToInt32(Decimal) method, which converts the specified decimal number value to the nearest 32-bit signed integer.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 5.5f; int i = Convert.ToInt32(f); Console.WriteLine(i); // 6 } } |
4. Using Math.Floor() method
Finally, if you need to round down a floating-point number to a nearest int, use Math.Floor() method. It returns the largest integer value less than or equal to the specified number.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 5.51f; int i = (int)Math.Floor(f); Console.WriteLine(i); // 5 } } |
Note that this is equivalent to explicit conversion using the casting, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 5.51f; int i = (int) f; Console.WriteLine(i); // 5 } } |
That’s all about converting a floating-point number to the nearest int in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)