HOWTO · Csharp

How to Convert Double to String in C#

In this guide, we will learn how we can convert a double into a string in C#.

On this page

To convert a C# double value to a string, call ToString(), use String.Format, or place the value in an interpolated string. These forms all produce text, while a format specifier such as F2 also controls how the number is displayed. Numeric formatting uses a culture, so choose one explicitly when the decimal separator must be predictable.

Use ToString() to convert a double to a string

Call ToString() on the value and assign the returned text to a string. This is the direct choice when the default numeric representation is suitable. The exact formatting can depend on the current culture; see the final section when output must be stable across machines.

Run this complete program to convert and print a value:

using System;

class Program
{
    static void Main()
    {
        double value = 0.09434;
        string text = value.ToString();
        Console.WriteLine(text);
    }
}
0.09434

The call creates text from the numeric value; it does not change value. Use the resulting string for display, logging, or another text-based operation.

Use String.Format to convert and format a double

String.Format inserts values into a composite format string. The {0} placeholder refers to the first value argument. With no format specifier, the value receives its default numeric formatting, much like ToString().

To request two digits after the decimal separator, add :F2 to the placeholder. This changes the displayed precision in the resulting text; it does not round or replace the original double value stored in the variable.

This complete program illustrates the formatted conversion:

using System;

class Program
{
    static void Main()
    {
        double value = 3.14159265359;
        string text = String.Format("{0:F2}", value);
        Console.WriteLine(text);
    }
}
3.14

Use string interpolation to convert and format a double

An interpolated string places an expression inside braces in a string literal prefixed with $. Assigning the interpolated value to a string performs the conversion as part of building that text. Add a format specifier after a colon to control its display, as in {value:F2}.

Here is the same two-decimal formatting expressed with interpolation:

using System;

class Program
{
    static void Main()
    {
        double value = 3.14159265359;
        string text = $"{value:F2}";
        Console.WriteLine(text);
    }
}
3.14

Choose interpolation when embedding a value in surrounding text is clearest. Choose String.Format when a composite format string is useful, or ToString() for a direct conversion. These alternatives format a value as text; they do not perform arithmetic on the original number.

Choose a format and culture for numeric output

The default numeric format uses the current culture. For example, cultures can represent the decimal separator differently. This matters when text is exchanged with another system or must have the same representation regardless of the machine’s settings.

Pass CultureInfo.InvariantCulture to request culture-independent formatting. The following complete program combines that choice with F2:

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        double value = 3.14159265359;
        string text = value.ToString("F2", CultureInfo.InvariantCulture);
        Console.WriteLine(text);
    }
}
3.14

Use current-culture formatting for text intended for a person in the application’s locale. Use invariant formatting for a stable representation that is exchanged or stored according to an application-defined format. For parsing or serialization, use the corresponding culture-aware APIs and format rules instead of assuming that display text is a universal numeric representation.