---
schema_version: "1"
id: 78c24a93e57924d8086a6361cf29e8dddd1120244fe21bd5106b267869d13a27
title: Integer Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_methods_system_integer.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 85ae82eb76ba41d05b2da7e8a944a98d3606ee170c21f86c85d55ceda33531f7
description: Contains methods for the Integer primitive data type.Returns the integer as a string using the locale of the context user.Returns an Integer that contains the value of the specified String. As in Java, the String is interpreted as representing a signed decimal integer.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.
---
# Integer Class

Contains methods for the Integer primitive data type.

## Namespace

[System](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_System.htm "The System namespace provides classes and methods for core Apex functionality.")

## Usage

For more information on integers, see [Integer Data Type](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/langCon_apex_primitives.htm).

## Integer Methods

The following are methods for Integer.

- **[format()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_System_Integer_format)**  
    Returns the integer as a string using the locale of the context user.
- **[valueOf(stringToInteger)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_System_Integer_valueOf)**  
    Returns an Integer that contains the value of the specified String. As in Java, the String is interpreted as representing a signed decimal integer.
- **[valueOf(fieldValue)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_System_Integer_valueOf_2)**  
    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.

### format()

Returns the integer as a string using the locale of the context user.

#### Signature

public String format()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Example

integer myInt = 22;
Assert.areEqual('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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [Integer](#apex_methods_system_integer "Contains methods for the Integer primitive data type.")

#### Examples

Integer myInt = Integer.valueOf('123');

A TypeException is returned if you attempt to convert a string to an invalid integer.

String n = 'NotAnInteger';
try {
    Integer myInt = Integer.valueOf(n);
} catch (TypeException ex) {
    System.debug(LoggingLevel.Error, ex.getMessage());
}

### 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](#apex_methods_system_integer "Contains methods for the Integer primitive data type.")

#### 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

List<AccountHistory> ahlist = 
  \[SELECT Field,OldValue,NewValue
   FROM AccountHistory WITH USER\_MODE\];
for(AccountHistory ah : ahlist) {
  System.debug('Field: ' + ah.Field);
  if (ah.field == 'NumberOfEmployees') {
    Integer oldValue = 
      Integer.valueOf(ah.OldValue);
    Integer newValue = 
      Integer.valueOf(ah.NewValue);
  }
}