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.
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.
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:
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:
- How to convert number with comma into decimal in C#?
- How to Convert Char Array to String in C#.NET?
- Convert String to Byte Array in C#
- Convert Object to Comma Separated String in C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…