Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

C#/.NET - convert decimal to string

0 contributions
11 points

In C#/.NET number string can be created in few ways.

1. ToString example

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

Output:

3.14

2. Convert.ToString example

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

Output:

3.14

3. string.Format example

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

Output:

3.14

4. String interpolation example

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

Output:

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

5. Summing string and number example (empty string)

decimal value = 3.14m;
string text = "" + value; 
// string text = value + "";
// string text = string.Empty + value; 
// string text = value + string.Empty;
Console.WriteLine(text); // 3.14m;

Output:

3.14

6. StringBuilder example

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

Output:

3.14

7. TypeConverter.ConvertTo example

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

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

Output:

3.14
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