While working on the Account object, I created the Account ID field using the Integer data type. However, the sales team wanted to store the account ID in alphanumeric form, which was not possible with the integer data type.
I was required to convert the Integer data type to a string in Salesforce Apex. In this tutorial, I will explain how I achieved this requirement and converted the integer data type to a string using different Apex methods.
Integer Data Type in Salesforce Apex
In Salesforce, Integer data type variables can store positive and negative numeric values. It is used to store short numbers or non-fractional values, which can be stored within the -2117483648 to +2117483647 range.
This data type has another subtype, Long. The extended data type is used to store more extended numbers, which can store values from -2^63 to +2^63-1. If you want to store big numbers, then you can use long data types.
Syntax:
integer i = 50;
long no = 2145689464654L; // enter L as a suffix, either capital or small.
Convert Integer Data Type to String in Salesforce Apex
In Salesforce, we can convert an Integer data type to a String using several methods. I explained some methods in the following Apex program.
1. Convert Integer to String Using String.valueOf() Method
public class ConvertDataType {
public void IntegerToString(){
Integer no = 150;
String strValue = String.valueOf(no);
System.debug('The Interger has been sucessfully converted to the String: ' +strValue);
}
}
Here, I created a variable named no with an Integer as the data type, and we wanted to convert it to a string. For that, we have String.valueOf() method. We need to pass an integer variable as a parameter in the method.
The method converts integer variables to the string data type and stores the result in the strValue variable, which is of the String data type.

The output strValue variable displays the integer variable value even though it is a string, which means the Integer data type has been successfully converted to a String.
In this way, we can convert an Integer data type to a string using the String.valueOf() method in Salesforce Apex.
2. Convert an Integer to a String Using the Format() Method
public void IntegerToString() {
Integer no = 123;
String str = no.format();
System.debug(str);
}
I created no variable as an Integer data type and str as a String. Then, using the format method, we need to add an integer variable to the format method and store the converted value in the str