How to Convert int to float with 2 Decimal Places in C#?

In this C# tutorial, I will show you how to convert int to float with 2 decimal places in C# with various examples. I will show you three different methods to convert int to float with 2 decimal places, using standard casting, the ToString() method, and the String.Format() method.

Convert int to float with 2 Decimal Places in C#

Check the three methods for c# convert int to float with 2 decimal places.

1- ToString() Method

Standard casting is the simplest way to convert an integer to a float. In C#, you can use explicit casting to convert data types, but since an integer can easily fit into a float, implicit conversion also works.

int myInt = 25;
float myFloat = (float) myInt; //Explicit conversion

However, to restrict it to 2 decimal places, you’ll need to format it, as shown below:

float myFormattedFloat = (float) Math.Round(myFloat, 2);

Here is a complete example with C#.

using System;

namespace ConvertIntToFloat
{
    class Program
    {
        static void Main(string[] args)
        {
            int myInt = 25;
            float myFloat = (float) myInt; //Explicit conversion
            float myFormattedFloat = (float) Math.Round(myFloat, 2);
            Console.WriteLine($"Converted and formatted float: {myFormattedFloat}");
        }
    }
}

You can see the output below in the screenshot after I run the code using Visual Studio.

Convert int to float with 2 Decimal Places in C#

2- Using ToString()

The ToString() method, is another technique to convert an integer to a float with 2 decimal places in C#.

int myInt = 25;
string floatString = myInt.ToString() + ".00";
float myFloat = float.Parse(floatString);

Here is a complete example:

using System;

namespace ConvertIntToFloat
{
    class Program
    {
        static void Main(string[] args)
        {
            int myInt = 25;
            string floatString = myInt.ToString() + ".00";
            float myFloat = float.Parse(floatString);
            Console.WriteLine($"Converted and formatted float: {myFloat}");
        }
    }
}

You can check out the below code after I run the code using the Visual Studio Windows application.

c# convert int to float with 2 decimal places

3- Using String.Format()

The String.Format() method can be used to convert an integer to a float and format it to 2 decimal places simultaneously in C#.

int myInt = 25;
string formattedFloat = String.Format("{0:0.00}", myInt);
float myFloat = float.Parse(formattedFloat);

Here is the complete code,

using System;

namespace ConvertIntToFloat
{
    class Program
    {
        static void Main(string[] args)
        {
            int myInt = 25;
            string formattedFloat = String.Format("{0:0.00}", myInt);
            float myFloat = float.Parse(formattedFloat);
            Console.WriteLine($"Converted and formatted float: {myFloat}");
        }
    }
}

After I run the code using Visual Studio, you can see the output below:

How to Convert int to float with 2 Decimal Places in C#

Conclusion

I hope you understand converting an integer to a float with 2 decimal places in C#. I have explained the below 3 methods of how to convert an int to float with 2 decimal places in C#.

1- ToString() Method
2- Using ToString()
3- Using String.Format()

You may also like the following tutorials: