Updated April 13, 2023
Introduction to Convert int to String C#
Converting int to String in C# defined as formatting numbers into a single string value. Purpose of this converting int to Strings is most of them by default accepting value is a String type and after receiving required String then we can convert into int type again. By this, we can overcome type format problems. Converting int to String achieves type safety.
How to Convert int to String in C#?
Converting can be done in many ways. We will come of the ways to convert int to String.
- int to string conversion
- int to string with Int32.ToString()
- int to string with string concatenation
- int to string with StringBuilder
- int to string with Convert.ToString()
- int to string with string.Format()
1. int to string conversion
Integer to String conversion is the type of typecasting or type conversion. This can convert non-decimal numbers to the string value.
Syntax:
int number=100;
String stringNumber=number.ToString();
2. int to string with Int32.ToString()
The Int32.ToString() method converts the non-decimal values into equivalent string characters.
Syntax:
int number=Int32.MaxValue;
// creating and initializing the object of CultureInfo
CultureInfo provider = new CultureInfo("fr-FR");
// declaring and intializing format
string format = "D5";
// using the method
string str = number.ToString(format, provider);
3. int to string with string concatenation
We can use the +(plus) operator in between String and int arguments, then the C# compiler automatically performs type conversion internally.
Syntax:
int number =214;
string output = "Converted number is" + number + " and now it is a string";
4. int to string with StringBuilder
String builder is used to performing mutability of the strings. We can also append integer values to StringBuilder to convert int to String.
Syntax:
int num = 400;
var stringBuilder = new StringBuilder();
stringBuilder.Append(num).ToString();
5. int to string with Convert.ToString()
We can also use Convert.ToString() method to convert int to string.
Syntax:
int num=500;
string s=Convert.ToString(num);
6. int to string with string.Format()
We can also use string.Format() method to convert int to string.
Syntax:
int num=800;
string s=string.Format(num);
Examples of Convert int to String C#
Here are the following examples mention below
Example #1
int to string conversion
Code:
//including C# basic libraries
using System;
//creating class
public class ToStringClass {
public static void Main(string[] args) {
//declaring String variable
String strinToNumber;
//declaring and initializing int variable
int number = 500;
//Converting int to string by using ToString() method
strinToNumber = number.ToString();
//Display output
Console.WriteLine("Converting int to String by using ToString() method result is = "+strinToNumber);
}
}
Output:
Example #2
int to string with Int32.ToString()
Code:
//including C# basic libraries
using System;
//including CultureInfo class
using System.Globalization;
//creating class
public class Int32ToStringClass {
public static void Main(string[] args) {
//Declaring and initializing int variable
int number=Int32.MaxValue;
// creating and initializing the object of CultureInfo
CultureInfo provider = new CultureInfo("fr-FR");
// declaring and intializing format
string format = "D5";
// Converting int to string by using Int32.ToString() method
string str = number.ToString(format, provider);
//Display the output
Console.WriteLine("Converting int to String by using Int32.ToString() method result is = "+str);
}
}
Output:
Example #3
int to string with string concatenation
Code:
//including C# basic libraries
using System;
//creating class
public class CancatClass {
public static void Main(string[] args) {
//Declaring and initializing int variable
int number=555;
//Display the output
Console.WriteLine("Converting int to String by using cancat operation(+) result is = "+number+" and now it is becomes string");
}
}
Output:
Example #4
int to string with StringBuilder
Code:
//including C# basic libraries
using System;
//including StringBuilder class package
using System.Text;
//creating class
public class StringBuilderClass {
public static void Main(string[] args) {
//Declaring and initializing int variable
int number=707;
//creating StringBuilder object
var stringBuilder = new StringBuilder();
//Adding int value to Strigbuilder to make int as String
stringBuilder.Append(number);
//Display the output
Console.WriteLine("Converting int to String by using StringBuilder class then result is = "+stringBuilder);
}
}
Output:
Example #5
int to string with Convert.ToString()
Code:
//including C# basic libraries
using System;
//creating class
public class CovertToStringClass {
public static void Main(string[] args) {
//Declaring and initializing int variable
int number=989;
//Converting int to string byy using Convert.ToString() method
string output=Convert.ToString(number);
//Display the output
Console.WriteLine("Converting int to String by using Convert.ToString() method then the result is = "+output);
}
}
Output:
Example #6
int to string with string.Format()
Code:
//including C# basic libraries
using System;
//creating class
public class StringFormatClass {
public static void Main(string[] args) {
//Declaring and initializing int variable
int number=214;
//Converting int to string byy using Convert.ToString() method
string outputString=string.Format("Converting int to String by using string.Format() method then the result is = "+number);
//Display the output
Console.WriteLine(outputString);
}
}
Output:
Conclusion
Converting int to string in C# is used to convert non-decimal numbers to string character. This can be done by using int to string conversion, int to string with Int32.ToString(), int to string with string concatenation, int to string with StringBuilder, int to string with Convert.ToString() and int to string with string.Format().
Recommended Articles
This is a guide to Convert int to String C#. Here we discuss how to convert int to String in C# with programming examples for better understanding. You may also have a look at the following articles to learn more –