Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

C#/.NET - convert string to float

0 contributions
10 points

In C#/.NET string can be parsed to float in few ways.

1. float.Parse example

string text = "3.14";
float value = float.Parse(text);
Console.WriteLine(value);

Output:

3.14

2. float.TryParse example

string text = "3.14";
float value;

if (float.TryParse(text, out value))
	Console.WriteLine(value);

Output:

3.14

3. Convert.ToFloat example

string text = "3.14";
float value = Convert.ToFloat(text);
Console.WriteLine(value);

Output:

3.14

4. TypeConverter.ConvertFrom example

TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));

string text = "3.14";
float value = (float)converter.ConvertFrom(text);

Console.WriteLine(value);

Output:

3.14

5. Notes

The following methods float.Parse, float.TryParse, Convert.ToFloat and TypeConverter.ConvertFrom throw System.FormatException.
0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ❤ 💻 🙂
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. 👍 ✔ 🦄
Read more