Skip to main content

How to Convert a Number to a String with ToString in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A number and the text that shows it are different things in C#: 3 is an int, and "3" is a string. Where text is needed, such as a JSON value, a label or a file, ToString() turns the number into its text. For numbers with decimals, which text you get depends on the culture.

Syntax​

Syntax
string <text> = <number>.ToString();
string <text> = <number>.ToString(CultureInfo.InvariantCulture);
PartWhat it does
<number>.ToString()The number as text, written the way the current culture writes numbers
ToString(CultureInfo.InvariantCulture)The number as text in a fixed, culture-independent form, with a . before the decimals. Needs using System.Globalization;

Microsoft's Int32.ToString reference: "Converts the numeric value of this instance to its equivalent string representation." For an int, it returns "The string representation of the value of this instance, consisting of a negative sign if the value is negative, and a sequence of digits ranging from 0 to 9 with no leading zeroes."

The Example​

The program turns a vote count and a negative number into text, then a price with decimals: first in this machine's culture, then in German, then in the invariant culture.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which turns votes 3 into text with ToString and prints it with votes, prints -42 with ToString, prints a price of 2.5 with ToString, then switches the current culture to de-DE and prints the price with ToString and with ToString of CultureInfo.InvariantCulture.

main.cs
using System.Globalization;

int votes = 3;
string text = votes.ToString();
Console.WriteLine(text + " votes");
Console.WriteLine((-42).ToString());

double price = 2.5;
Console.WriteLine(price.ToString());

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine(price.ToString());
Console.WriteLine(price.ToString(CultureInfo.InvariantCulture));

CultureInfo.CurrentCulture = ... switches this program to German number formats, the way a German computer would be set up.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
3 votes
-42
2.5
2,5
2.5

A zsh terminal where dotnet run main.cs prints 3 votes, -42, 2.5, 2,5 and 2.5.

  1. votes.ToString() gave "3", which + joined with " votes".
  2. -42 kept its minus sign.
  3. The price printed as 2.5 in this machine's culture, then as 2,5 in German, which writes a comma before the decimals.
  4. With CultureInfo.InvariantCulture, it was 2.5 again, whatever the current culture.

The command is the same in zsh, bash and PowerShell.

A Number Isn't a String​

C# doesn't turn a number into text by itself when you assign it:

main.cs
int votes = 3;
string text = votes;
Output
/Users/you/demo/main.cs(2,15): error CS0029: Cannot implicitly convert type 'int' to 'string'

The build failed. Fix the build errors and run again.

Call ToString(): string text = votes.ToString();.

Common Mistakes​

MistakeWhat happensDo this instead
string text = votes;error CS0029: Cannot implicitly convert type 'int' to 'string'votes.ToString()
Writing decimals with ToString() for another program to readOn a computer set to German, 2.5 comes out as 2,5ToString(CultureInfo.InvariantCulture)
Forgetting using System.Globalization;error CS0103: The name 'CultureInfo' does not exist in the current contextAdd the using line at the top

Troubleshooting​

SymptomLikely causeFix
A comma where you expected a decimal pointThe current culture writes decimals with a commaPass CultureInfo.InvariantCulture
CS0029 ... 'int' to 'string'A number assigned to a stringCall ToString()

FAQ​

Do I need ToString when I join a number to text?​

No. With + and a string on one side, C# converts the number for you: "Votes: " + votes printed Votes: 3. String interpolation does the same, and uses the current culture too: under German, $"Price: {price}" printed Price: 2,5.

Does the culture change whole numbers too?​

For these, no: under German, 3.ToString() and (-42).ToString() still printed 3 and -42. Cultures differ mostly in decimals, and in the separators that formats such as "N0" add.

Conclusion​

number.ToString() turns a number into text in the current culture's style, and ToString(CultureInfo.InvariantCulture) into the same text everywhere, which is what other programs expect. Assigning a number straight to a string is error CS0029.

Additional Resources​