Languages
[Edit]
EN

C#/.NET - convert double to string

2 points
Created by:
AnnLen
1771

In C#/.NET double can be converted to string in few ways.

1. ToString example

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

Output:

3.14

2. Convert.ToString example

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

Output:

3.14

3. string.Format example

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

Output:

3.14

4. String interpolation example

double value = 3.14;
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)

double value = 3.14;
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

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

Output:

3.14

7. TypeConverter example

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

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

Output:

3.14

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists 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,
    this community will help you.
Rockstar community members
Thank you for great work 👍