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 float to string

0 contributions
4 points

1. ToString example

float value = 3.14f;
string text = value.ToString(); // or 3.14f.ToString()
Console.WriteLine(text); // 3.14

Output:

3.14

2. Convert.ToString example

float value = 3.14f;
string text = Convert.ToString(value);
Console.WriteLine(text); // 3.14

Output:

3.14

3. string.Format example

float value = 3.14f;
string text = string.Format("{0}", value);
Console.WriteLine(text); // 3.14

Output:

3.14

4. String interpolation example

float value = 3.14f;
string text = $"{value}";
Console.WriteLine(text); // 3.14

Output:

3.14
Note: this feature available is in C# 6 and later versions 

5. Summing string and number example (empty string)

float value = 3.14f;
string text = "" + value;
// string text = value + "";
// string text = string.Empty + value; 
// string text = value + string.Empty;
Console.WriteLine(text); // 3.14

Output:

3.14

6. StringBuilder example

float value = 3.14f;
string text = new StringBuilder().Append(value).ToString();
Console.WriteLine(text); // 3.14

Output:

3.14

7. TypeConverter.ConvertTo example

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

float value = 3.14f;
string text = (string)converter.ConvertTo(value, typeof(string));
Console.WriteLine(text); // 3.14

Output:

3.14
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more