Newer Version Available
Integer Class
Contains methods for the Integer primitive data type.
Namespace
Usage
For more information on integers, see Integer Data Type.
Integer Methods
The following are methods for Integer.
format()
Returns the integer as a string using the locale of the
context user.
Signature
public String format()
Return Value
Type: String
Example
1integer myInt = 22;
2system.assertEquals('22', myInt.format());valueOf(stringToInteger)
Returns an Integer that contains the value of the specified
String. As in Java, the String is interpreted as representing a signed
decimal integer.
Signature
public static Integer valueOf(String stringToInteger)
Parameters
- stringToInteger
- Type: String
Return Value
Type: Integer
Examples
1Integer myInt = Integer.valueOf('123');A TypeException is returned if you attempt to convert a string to an invalid integer.
1String n = 'NotAnInteger';
2try {
3 Integer myInt = Integer.valueOf(n);
4} catch (TypeException ex) {
5 System.debug(LoggingLevel.Error, ex.getMessage());
6}valueOf(fieldValue)
Converts the specified object to an Integer. Use this method to convert a history
tracking field value or an object that represents an Integer value.
Signature
public static Integer valueOf(Object fieldValue)
Parameters
- fieldValue
- Type: Object
Return Value
Type: Integer
Usage
Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field type corresponds to an Integer type, like a number field.
Example:
Example
1List<AccountHistory> ahlist =
2 [SELECT Field,OldValue,NewValue
3 FROM AccountHistory];
4for(AccountHistory ah : ahlist) {
5 System.debug('Field: ' + ah.Field);
6 if (ah.field == 'NumberOfEmployees') {
7 Integer oldValue =
8 Integer.valueOf(ah.OldValue);
9 Integer newValue =
10 Integer.valueOf(ah.NewValue);
11}