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:

Download  Run Code

 
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.

Download  Run Code

 
2. Fixed-point format specifier (F)

Download  Run Code

 
3. Numeric format specifier (N) with group separators

Download  Run Code

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.

Download  Run Code

 
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.

Download  Run Code

 
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:

Download  Run Code

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).

Download  Run Code

That’s all about rounding float to 2 decimal points in C#.