In the below guide, the ways to convert a C# string to a float datatype are discussed.
Convert String To Float In C#
A string contains data that can be numeric, alphabetic, or a combination of both. We can convert a string to other datatypes but the only requirement for this conversion is that the string must contain the data that is in the required data type format. This means for the conversion of a “string” to ‘float” the string must contain only float numbers otherwise it will not be converted. There are a few methods that will help to convert a string to float:
- “float.Parse()” Method
- “float.TryParse()” Method
- “Single.Parse()” Method
- “Single.TryParse()” Method
- “Convert.ToSingle()” Method
Method 1: Using “float.Parse()” Method for the Conversion of String to Float
The “Parse()” method in C# is mainly used for the conversion of a string to other datatypes. To convert it to float we use this method with the ‘float” keyword. Here is the syntax:
This method takes two arguments, first one is the name of the variable which is in the form of a “string” and the second one is the culture info that will tell the format of the float value. The following example will show its working:
using System.Globalization;
class String_to_float_example {
static void Main(string[] args) {
string a = "123.445566";
float b = float.Parse(a, CultureInfo.InvariantCulture.NumberFormat);
Console.WriteLine(b);
}
}
The above-stated code shows:
- A “System” namespace is used along with “System.Globalization”.
- Then a class “String_to_float_example” is declared that has a “Main()” method.
- After that, there is a string “a” having value “123.445566”.
- A float variable “b” is declared that uses the “float.Parse()” method.
- And lastly the value of “b” is printed which is the value in float type.
This “123.4456” is of float type.
Method 2: Using “float.TryParse()” Method for the Conversion of String to Float
It is always suggested to use “TryParse()” instead of “Parse()” because some values are not able to convert if we use the “TryParse()” method it will first check whether the value is able to convert it and then convert that value. The following example will help to understand this:
using System.Globalization;
class String_to_float_example {
static void Main(string[] args) {
string a = "123.445566";
string c="A123.3333";
float b;
if(float.TryParse(a, out b)){
Console.WriteLine("Successfully converted to float. The value in type float is: "+b);
}
else{
Console.WriteLine("The Value can not be converted.");
}
if(float.TryParse(c, out b)){
Console.WriteLine("Successfully converted to float. The value in type float is: "+b);
}
else{
Console.WriteLine("The Value can not be converted to float.");
}
}
}
Here, the previous example is used. Additionally,
- A new variable “c” is declared with the value “A123.3333“.
- Then two “if-else” loops are used.
- Whether or not “a” includes a float value will be determined by the first loop. If the value of “a” contains a float number then the number will be converted to a float datatype.
- The second loop will check “c” the same way.
- If the values of “a” or “c” contains any letter then “The Value can not be converted to float.” message will e shown.
Output:
Here the output shows that:
- “a” s successfully convert to float type as it contains only numeric data.
- “C” is not converted to float type as it contains the letter “A”.
Method 3: Using “Single.Parse()” Method for the Conversion of String to Float
In Base Class Library the name for float is “Single” and also float is the alias for “System.Single type” in C#. therefore “Single” is also used with “Parse()” method in C# for conversion of string to float. The sole distinction between “float.Parse()” and “Single.Parse()” is that “float.Parse()” requires two arguments since we must specify the format, but “Single.Parse()” simply requires the variable representing the input text as an argument. The syntax of “Single.Parse()” method is:
To understand the working of this method see the following example:
using System.Globalization;
class String_to_float_example {
static void Main(string[] args) {
string a = "123.445566";
float b = Single.Parse(a);
Console.WriteLine(b);
}
}
In the above example:
- The class “String_to_float_example” has a method “Main()” which has a variable “a” having value “123.445566“.
- There is another variable “b” of type float which uses “Single.Parse()” method to convert the string “a” to float and then its value is shown as output.
Output:
Method 4: Using “Single.TryParse()” Method for the Conversion of String to Float
Like “float.TryParse()”, this method also works in the same way and is used to check whether the conversion is possible or not otherwise the compiler will show an error. Check out the example below:
using System.Globalization;
class String_to_float_example {
static void Main(string[] args) {
string a = "123.445566";
string c="A123.3333";
float b;
if(Single.TryParse(a, out b)){
Console.WriteLine("Successfully converted to float. The value in type float is: "+b);
}
else{
Console.WriteLine("The Value can not be converted.");
}
if(Single.TryParse(c, out b)){
Console.WriteLine("Successfully converted to float. The value in type float is: "+b);
}
else{
Console.WriteLine("The Value can not be converted to float.");
}
}
}
The same example used in the “float.TryParse()” method is also used here the only difference is:
- The method “float.TryParse()” is replaced with the “Single.TryParse()” method.
Let’s verify the output:
The output is also the same. So both method works in the same way.
Method 5: Using the “Convert.ToSingle()” Method for the Conversion of String to Float
“Convert” class in C# helps to convert various base datatypes into another datatype. To convert a string datatype to float “Convert.ToSingle()” method is used. To demonstrate how this method functions, try the example below:
using System.Globalization;
class String_to_float_example {
static void Main(string[] args) {
string a = "123.445566";
float b = Convert.ToSingle(a);
Console.WriteLine(b);
}
}
In this code:
- A string “a” is declared that has an assigned value “123.445566“.
- Then float variable “b” is declared in which the “Convert.ToSingle()” method is called by passing “a” s its argument.
- Lastly the value of “b” is printed.
Output:
Conclusion
In C# various base datatypes can be converted to other datatypes using different methods and classes. To convert a “string”, the “Parse()” method is used but first it is necessary to verify if the conversion is possible or not, for this purpose “TryParse()” method is used. The methods “float.Parse(),” “float.TryParse()”, “Single.Parse()“, “Single.TryParse()“, and “Convert.ToSingle()” are used to transform a “string” into a “float”.