Round float to 2 decimal points in C#
This post will discuss how to round float to 2 decimal points in C#.
1. Using ToString() method
We can use the ToString() method to format a floating-point value to some decimal places. The ToString() method accepts a numeric format string and converts the current instance value to an equivalent string. To restrict a float to 2-decimal places, you can use the #.## format specifier, 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 = 3.14285f; string s = f.ToString("#.##"); Console.WriteLine(s); // 3.14 } } |
C# offers several standard format specifiers, that can be used to round a floating-point value to 2-decimal places. These are discussed below:
1. “0” custom format specifier.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 3.14285f; string s = f.ToString("0.00"); Console.WriteLine(s); // 3.14 } } |
2. Fixed-point format specifier (F)
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { float f = 3.14285f; string s = f.ToString("F2"); Console.WriteLine(s); // 3.14 } } |
3. Numeric format specifier (N) with group separators
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { float f1 = 3.14285f; Console.WriteLine(f1.ToString("N2")); // 3.14 float f2 = 10000.874287f; Console.WriteLine(f2.ToString("N2")); // 10,000.87 } } |
2. Using Decimal.Round() method
Another common approach is to use the Decimal.Round() method for rounding a value to the specified number of decimal digits. The advantage of using this method is that it retains the value as Decimal, instead of returning a string. The following example illustrates.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { decimal d = 3.14285m; d = Decimal.Round(d, 2); Console.WriteLine(d); // 3.14 } } |
You can also specify the strategy for rounding the decimal number using MidpointRounding Enum. For example, the following code uses the rounding convention of MidpointRounding.AwayFromZero.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { decimal d1 = 3.505m; d1 = Decimal.Round(d1, 2); Console.WriteLine(d1); // 3.50 decimal d2 = 3.505m; d2 = Decimal.Round(d2, 2, MidpointRounding.AwayFromZero); Console.WriteLine(d2); // 3.51 } } |
The Math class also provides the Round() method for rounding a value to the specified number of fractional digits. The following example provides the output with different inputs:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; public class Example { public static void Main() { float f1 = 3.14285f; double d1 = Math.Round(f1, 2); Console.WriteLine(d1); // 3.14 float f2 = 3.1000f; double d2 = Math.Round(f2, 2); Console.WriteLine(d2); // 3.1 float f3 = 3.0000f; double d3 = Math.Round(f3, 2); Console.WriteLine(d3); // 3 } } |
3. Using String.Format() method
Similar to the ToString() method, you can use the String.Format() method for rounding a floating-point value to 2 decimal points. The following example illustrates using #.## format specifier, 0 custom format specifier, fixed-point format specifier (F), and numeric format specifier (N).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; public class Example { public static void Main() { float f = 3.14285f; string s = String.Format("{0:0.00}", f); Console.WriteLine(s); // 3.14 s = String.Format("{0:#.##}", f); Console.WriteLine(s); // 3.14 s = String.Format("{0:F2}", f); Console.WriteLine(s); // 3.14 s = String.Format("{0:N2}", f); Console.WriteLine(s); // 3.14 } } |
That’s all about rounding float to 2 decimal points 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 :)