Home > Backend Development > C#.Net Tutorial > Convert int to String C#

Convert int to String C#

WBOY
Release: 2024-09-03 15:17:13
Original
515 people have browsed it

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();
Copy after login

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);
Copy after login

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";
Copy after login

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();
Copy after login

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);
Copy after login

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);
Copy after login

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);
}
}
Copy after login

Output:

Convert int to String C#

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);
}
}
Copy after login

Output:

Convert int to String C#

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");
}
}
Copy after login

Output:

Convert int to String C#

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);
}
}
Copy after login

Output:

Convert int to String C#

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);
}
}
Copy after login

Output:

Convert int to String C#

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);
}
}
Copy after login

Output:

Convert int to String C#

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().

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template