C# How to Convert String to Float with Examples
In C#, convert a string to a float using
float.Parse(stringValue) or safely with float.TryParse(stringValue, out float result).Examples
Input"3.14"
Output3.14
Input"-0.001"
Output-0.001
Input"abc"
OutputConversion failed (TryParse returns false)
How to Think About It
To convert a string to a float, first consider if the string is always a valid number. If yes, use
float.Parse which throws an error if invalid. If not sure, use float.TryParse which returns true or false and avoids errors.Algorithm
1
Get the input string.2
Try to convert the string to a float using a safe method.3
If conversion succeeds, return the float value.4
If conversion fails, handle the error or return a default value.Code
csharp
using System; class Program { static void Main() { string input = "3.14"; if (float.TryParse(input, out float number)) { Console.WriteLine(number); } else { Console.WriteLine("Conversion failed."); } } }
Output
3.14
Dry Run
Let's trace converting the string "3.14" to float using TryParse.
1
Input string
input = "3.14"
2
TryParse conversion
float.TryParse("3.14", out number) returns true and number = 3.14
3
Output result
Print 3.14
| Input String | TryParse Result | Output Number |
|---|---|---|
| 3.14 | true | 3.14 |
Why This Works
Step 1: Using float.Parse
float.Parse converts a valid numeric string to float but throws an exception if the string is invalid.
Step 2: Using float.TryParse
float.TryParse attempts conversion and returns a boolean indicating success, avoiding exceptions.
Step 3: Output handling
If conversion succeeds, the float value is used; otherwise, you can handle failure gracefully.
Alternative Approaches
Using float.Parse with try-catch
csharp
using System; class Program { static void Main() { string input = "3.14"; try { float number = float.Parse(input); Console.WriteLine(number); } catch (FormatException) { Console.WriteLine("Invalid input string."); } } }
This method throws an exception on invalid input, so you must handle errors with try-catch.
Using Convert.ToSingle
csharp
using System; class Program { static void Main() { string input = "3.14"; try { float number = Convert.ToSingle(input); Console.WriteLine(number); } catch (FormatException) { Console.WriteLine("Invalid input string."); } } }
Convert.ToSingle also converts string to float but throws exceptions on invalid input.
Complexity: O(1) time, O(1) space
Time Complexity
Conversion is a simple parsing operation that runs in constant time.
Space Complexity
Only a few variables are used, so space is constant.
Which Approach is Fastest?
float.Parse and float.TryParse have similar speed; TryParse is safer for unknown input.
| Approach | Time | Space | Best For |
|---|---|---|---|
| float.Parse | O(1) | O(1) | Known valid strings, simple code |
| float.TryParse | O(1) | O(1) | Unknown input, safe conversion |
| Convert.ToSingle | O(1) | O(1) | Similar to Parse, with exception handling |
Use
float.TryParse to safely convert strings without risking exceptions.Trying to convert invalid strings with
float.Parse without error handling causes runtime exceptions.