Mohanapriya R Mohanapriya R
Updated date Jul 30, 2024
In this article, we will explore the ins and outs of converting integers to strings in C#. Learn the basics of the ToString method, discover customization through format specifiers.
  • 1.1k
  • 0
  • 0

Converting Integers to Strings in C#:

We can use ToString method to convert the integer to a string in C#. Here is the C# program.

using System;

class Program
{
    static void Main()
    {
        // Integer value
        int myNumber = 60;

        // Convert integer to string using ToString method
        string myString = myNumber.ToString();

        // Display the results
        Console.WriteLine("Integer: " + myNumber);
        Console.WriteLine("String: " + myString);
    }
}

Output:

Integer: 60
String: 60

The integer value 60 is converted to its string representation using the ToString method as shown above.

Customizing Integer to String Conversion:

The ToString method allows for additional customization using format specifiers. For example, you can format the string to include leading zeros or specify a specific number of decimal places for floating-point numbers. Here's an example:

int myNumber = 1234;
string formattedString = myNumber.ToString("D5"); // Display as 5-digit number with leading zeros
Console.WriteLine("Formatted String: " + formattedString);

In this case, the format specifier "D5" ensures that the integer is displayed as a 5-digit number with leading zeros.

Output:

Formatted String: 01234

Comments (0)

There are no comments. Be the first to comment!!!