Convert int to string?
Categories:
Converting Integers to Strings in C#

Learn various methods to convert integer data types to string representations in C#, understanding their nuances and best use cases.
Converting an integer to its string representation is a common task in C# programming. Whether you need to display a number to a user, concatenate it with other text, or store it in a text-based format, C# provides several straightforward methods to achieve this. This article will explore the most common and efficient ways to perform this conversion, along with considerations for formatting and cultural differences.
The ToString()
Method: The Most Common Approach
The ToString()
method is the most direct and frequently used way to convert any value type, including integers, to its string equivalent. Every primitive type in C# inherits from System.Object
, which provides a default ToString()
implementation. For numeric types, this method is overridden to return a string representation of the number.
int number = 123;
string strNumber = number.ToString(); // strNumber will be "123"
Console.WriteLine(strNumber);
Basic usage of ToString()
for integer to string conversion.
ToString()
method also supports format specifiers and IFormatProvider
(like CultureInfo
) for more control over the output string, especially useful for currency, percentages, or locale-specific number formats.String Concatenation and Interpolation
C# offers convenient ways to embed integers directly into strings, which implicitly performs the conversion. String concatenation using the +
operator and string interpolation using the $
prefix are powerful features that handle the int
to string
conversion automatically.
int age = 30;
// Using string concatenation
string message1 = "My age is " + age; // message1 will be "My age is 30"
// Using string interpolation (C# 6.0 and later)
string message2 = $"My age is {age}"; // message2 will be "My age is 30"
Console.WriteLine(message1);
Console.WriteLine(message2);
Converting an integer to string using concatenation and interpolation.
StringBuilder
is generally more efficient, though it still relies on ToString()
internally for individual elements.Understanding the Conversion Process
At its core, converting an integer to a string involves representing the numerical value using character symbols. This process is handled by the .NET runtime and typically involves converting the binary representation of the integer into a sequence of Unicode characters that correspond to the decimal digits. The following diagram illustrates the conceptual flow of this conversion.
flowchart TD A[Integer Value (e.g., 123)] --> B{"Call ToString()"} B --> C{Internal Conversion Logic} C --> D[Character Array (e.g., ['1', '2', '3'])] D --> E[String Object (e.g., "123")] E --> F[Output/Usage]
Conceptual flow of integer to string conversion in C#.
Advanced Formatting with ToString()
The ToString()
method offers overloads that accept format specifiers and IFormatProvider
objects, allowing for precise control over the output string. This is crucial for internationalization and specific display requirements.
int value = 12345;
// Currency format (uses current culture's currency symbol and decimal places)
string currency = value.ToString("C"); // e.g., "$12,345.00" (for en-US)
// Number format with thousands separator
string numberFormat = value.ToString("N0"); // e.g., "12,345"
// Hexadecimal format
string hex = value.ToString("X"); // e.g., "3039"
// Using a specific culture
System.Globalization.CultureInfo frenchCulture = new System.Globalization.CultureInfo("fr-FR");
string frenchCurrency = value.ToString("C", frenchCulture); // e.g., "12 345,00 €"
Console.WriteLine($"Currency: {currency}");
Console.WriteLine($"Number Format: {numberFormat}");
Console.WriteLine($"Hexadecimal: {hex}");
Console.WriteLine($"French Currency: {frenchCurrency}");
Examples of advanced formatting with ToString()
.
IFormatProvider
explicitly or relying on CultureInfo.CurrentCulture
is vital for applications used internationally.