Home > Article > Backend Development > 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.
Converting can be done in many ways. We will come of the ways to convert int to String.
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();
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);
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";
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();
We can also use Convert.ToString() method to convert int to string.
Syntax:
int num=500; string s=Convert.ToString(num);
We can also use string.Format() method to convert int to string.
Syntax:
int num=800; string s=string.Format(num);
Here are the following examples mention below
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:
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:
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:
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:
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:
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:
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().
The above is the detailed content of Convert int to String C#. For more information, please follow other related articles on the PHP Chinese website!