---
schema_version: "1"
id: 681729020fea6211ad24ceac2d9ae2b97b1744a8b0afdce99192b5df8cffec01
title: Decimal Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_methods_system_decimal.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 9b8077915b5204a431349a5f8159bb7f54641a10854509d8d60e9845742d5d54
description: Contains methods for the Decimal primitive data type.Rounding mode specifies the rounding behavior for numerical operations capable of discarding precision.Returns the absolute value of the Decimal.Divides this Decimal by the specified divisor, and sets the scale, that is, the number of decimal places, of the result using the specified scale.Divides this Decimal by the specified divisor, sets the scale, that is, the number of decimal places, of the result using the specified scale, and if necessary, rounds the value using the rounding mode.Returns the Double value of this Decimal.Returns the String value of this Decimal using the locale of the context user. Returns the Integer value of this Decimal.Returns the Long value of this Decimal.Returns the value of this decimal raised to the power of the specified exponent.Returns the total number of digits for the Decimal.Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using half-even rounding mode, that is, it rounds towards the “nearest neighbor” unless both neighbors are equidistant, in which case, this mode rounds towards the even neighbor.Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using the rounding mode specified by the rounding mode. Returns the scale of the Decimal, that is, the number of decimal places.Returns the Decimal scaled to the specified number of decimal places, using half-even rounding, if necessary. Half-even rounding mode rounds toward the “nearest neighbor.” If both neighbors are equidistant, the number is rounded toward the even neighbor.Returns the Decimal scaled to the specified number of decimal places, using the specified rounding mode, if necessary.Returns the Decimal with any trailing zeros removed.Returns the String value of this Decimal, without using scientific notation.Returns a Decimal that contains the value of the specified Double.Returns a Decimal that contains the value of the specified Long.Returns a Decimal that contains the value of the specified String. As in Java, the string is interpreted as representing a signed Decimal.
---
# Decimal Class

Contains methods for the Decimal 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

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

Two Decimal objects that are numerically equivalent but differ in scale (such as 1.1 and 1.10) generally do not have the same hashcode. Use caution when such Decimal objects are used in Sets or as Map keys.

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

- **[Rounding Mode](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_decimal_rounding_mode)**  
    Rounding mode specifies the rounding behavior for numerical operations capable of discarding precision.
- **[Decimal Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_methods)**

## Rounding Mode

Rounding mode specifies the rounding behavior for numerical operations capable of discarding precision.

Each rounding mode indicates how the least significant returned digit of a rounded result is to be calculated. The following are the valid values for roundingMode.

<table class="featureTable sort_table" summary="">
              <thead class="thead sorted" align="left">
                <tr>
                  <th class="featureTableHeader vertical-align-top " id="d1607482e156">Name</th>

                  <th class="featureTableHeader vertical-align-top " id="d1607482e159">Description</th>

                </tr>

              </thead>

              <tbody class="tbody">
                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">CEILING</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Rounds towards positive infinity. That is, if the result is positive, this mode behaves the
                    same as the <samp class="codeph apex_code">UP</samp> rounding mode; if the result is
                    negative, it behaves the same as the <samp class="codeph apex_code">DOWN</samp> rounding
                    mode. Note that this rounding mode never decreases the calculated value. For example:<ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">CEILING</samp> round mode result:
                        6</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">CEILING</samp> round mode result:
                        2</li>

                      <li class="li">Input number -1.1: <samp class="codeph apex_code">CEILING</samp> round mode result:
                        -1</li>

                      <li class="li">Input number -2.7: <samp class="codeph apex_code">CEILING</samp> round mode result:
                        -2</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_ceiling">CEILING Rounding Mode Example</a>.</p>
                  </td>

                </tr>

                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">DOWN</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Rounds towards zero. This rounding mode always discards any fractions (decimal points) prior
                    to executing. Note that this rounding mode never increases the magnitude of the calculated
                    value. For example:<ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">DOWN</samp> round mode result:
                        5</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">DOWN</samp> round mode result:
                        1</li>

                      <li class="li">Input number -1.1: <samp class="codeph apex_code">DOWN</samp> round mode result:
                        -1</li>

                      <li class="li">Input number -2.7: <samp class="codeph apex_code">DOWN</samp> round mode result:
                        -2</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_down">DOWN Rounding Mode Example</a>.</p>
                  </td>

                </tr>

                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">FLOOR</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Rounds towards negative infinity. That is, if the result is positive, this mode behaves the
                    same as the<samp class="codeph apex_code">DOWN</samp> rounding mode; if negative, this
                    mode behaves the same as the <samp class="codeph apex_code">UP</samp> rounding mode. Note
                    that this rounding mode never increases the calculated value. For example:<ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">FLOOR</samp> round mode result:
                        5</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">FLOOR</samp> round mode result:
                        1</li>

                      <li class="li">Input number -1.1: <samp class="codeph apex_code">FLOOR</samp> round mode result:
                        -2</li>

                      <li class="li">Input number -2.7: <samp class="codeph apex_code">FLOOR</samp> round mode result:
                        -3</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_floor">FLOOR Rounding Mode Example</a>.</p>
                  </td>

                </tr>

                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">HALF_DOWN</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Rounds towards the “nearest neighbor” unless both neighbors are equidistant, in
                    which case this mode rounds down. This rounding mode behaves the same as the <samp class="codeph apex_code">UP</samp> rounding mode if the discarded fraction (decimal point)
                    is &gt; 0.5; otherwise, it behaves the same as <samp class="codeph apex_code">DOWN</samp>
                    rounding mode. For example:<ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">HALF_DOWN</samp> round mode result:
                        5</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">HALF_DOWN</samp> round mode result:
                        1</li>

                      <li class="li">Input number -1.1: <samp class="codeph apex_code">HALF_DOWN</samp> round mode
                        result: -1</li>

                      <li class="li">Input number -2.7: <samp class="codeph apex_code">HALF_DOWN</samp> round mode
                        result: -3</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_half_down">HALF_DOWN Rounding Mode Example</a>.</p>
                  </td>

                </tr>

                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">HALF_EVEN</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Rounds <span class="ph" id="half_even_defn1"><a name="half_even_defn1"><!-- --></a>towards the “nearest neighbor” unless both
                      neighbors are equidistant, in which case, this mode rounds towards the even neighbor.
                    </span>This rounding mode behaves the same as the <samp class="codeph apex_code">HALF_UP</samp> rounding mode if the digit to the left of the discarded fraction (decimal
                    point) is odd. It behaves the same as the <samp class="codeph apex_code">HALF_DOWN</samp>
                    rounding method if it is even. For example: <ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">HALF_EVEN</samp> round mode result:
                        6</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">HALF_EVEN</samp> round mode result:
                        1</li>

                      <li class="li">Input number -1.1: <samp class="codeph apex_code">HALF_EVEN</samp> round mode
                        result: -1</li>

                      <li class="li">Input number -2.7: <samp class="codeph apex_code">HALF_EVEN</samp> round mode
                        result: -3</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_half_even">HALF_EVEN Rounding Mode Example</a>.</p>
                    <span class="ph" id="half_even_defn2"><a name="half_even_defn2"><!-- --></a>Note that this rounding mode statistically minimizes cumulative error
                      when applied repeatedly over a sequence of calculations. </span>
                  </td>

                </tr>

                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">HALF_UP</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Rounds towards the “nearest neighbor” unless both neighbors are equidistant, in
                    which case, this mode rounds up. This rounding method behaves the same as the <samp class="codeph apex_code">UP</samp> rounding method if the discarded fraction (decimal
                    point) is &gt;= 0.5; otherwise, this rounding method behaves the same as the <samp class="codeph apex_code">DOWN</samp> rounding method. For example:<ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">HALF_UP</samp> round mode result:
                        6</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">HALF_UP</samp> round mode result:
                        1</li>

                      <li class="li">Input number -1.1: <samp class="codeph apex_code">HALF_UP</samp> round mode result:
                        -1</li>

                      <li class="li">Input number -2.7: <samp class="codeph apex_code">HALF_UP</samp> round mode result:
                        -3</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_half_up">HALF_UP Rounding Mode Example</a>.</p>
                  </td>

                </tr>

                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">UNNECESSARY</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Asserts that the requested operation has an exact result, which means that no rounding is
                    necessary. If this rounding mode is specified on an operation that
                    yields an inexact result, a MathException is thrown. For example: <ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">UNNECESSARY</samp> round mode result:
                        MathException</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">UNNECESSARY</samp> round mode result:
                        MathException</li>

                      <li class="li">Input number 1.0: <samp class="codeph apex_code">UNNECESSARY</samp> round mode result: 1</li>

                      <li class="li">Input number -1.0: <samp class="codeph apex_code">UNNECESSARY</samp> round mode result: -1</li>

                      <li class="li">Input number -2.2: <samp class="codeph apex_code">UNNECESSARY</samp> round mode result:
                        MathException</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_unnecessary">UNNECESSARY Rounding Mode Example</a>.</p>
                  </td>

                </tr>

                <tr>
                  <td class="entry" headers="d1607482e156" data-title="Name"><samp class="codeph apex_code">UP</samp></td>

                  <td class="entry" headers="d1607482e159" data-title="Description">Rounds away from zero. This rounding mode always truncates any fractions (decimal points)
                    prior to executing. Note that this rounding mode never decreases the magnitude of the
                    calculated value. For example:<ul class="ul bulletList">
                      <li class="li">Input number 5.5: <samp class="codeph apex_code">UP</samp> round mode result: 6</li>

                      <li class="li">Input number 1.1: <samp class="codeph apex_code">UP</samp> round mode result: 2</li>

                      <li class="li">Input number -1.1: <samp class="codeph apex_code">UP</samp> round mode result:
                        -2</li>

                      <li class="li">Input number -2.7: <samp class="codeph apex_code">UP</samp> round mode result:
                        -3</li>

                    </ul>
                    <p class="p">For an example, see <a class="xref" href="#ex_up">UP Rounding Mode Example</a>.</p>
                  </td>

                </tr>

              </tbody>

            </table>

### CEILING Rounding Mode Example

Decimal\[\] example = new Decimal\[\]{5.5, 1.1, -1.1, -2.7};
Long\[\] expected = new Long\[\]{6, 2, -1, -2};
for(integer x = 0; x < example.size(); x++){
    Assert.areEqual(expected\[x\],
        example\[x\].round(System.RoundingMode.CEILING));
}

### DOWN Rounding Mode Example

Decimal\[\] example = new Decimal\[\]{5.5, 1.1, -1.1, -2.7};
Long\[\] expected = new Long\[\]{5, 1, -1, -2};
for(integer x = 0; x < example.size(); x++){
    Assert.areEqual(expected\[x\],
        example\[x\].round(System.RoundingMode.DOWN));
}

### FLOOR Rounding Mode Example

Decimal\[\] example = new Decimal\[\]{5.5, 1.1, -1.1, -2.7};
Long\[\] expected = new Long\[\]{5, 1, -2, -3};
for(integer x = 0; x < example.size(); x++){
    Assert.areEqual(expected\[x\],
        example\[x\].round(System.RoundingMode.FLOOR));
}

### HALF\_DOWN Rounding Mode Example

Decimal\[\] example = new Decimal\[\]{5.5, 1.1, -1.1, -2.7};
Long\[\] expected = new Long\[\]{5, 1, -1, -3};
for(integer x = 0; x < example.size(); x++){
    Assert.areEqual(expected\[x\],
        example\[x\].round(System.RoundingMode.HALF\_DOWN));
}

### HALF\_EVEN Rounding Mode Example

Decimal\[\] example = new Decimal\[\]{5.5, 1.1, -1.1, -2.7};
Long\[\] expected = new Long\[\]{6, 1, -1, -3};
for(integer x = 0; x < example.size(); x++){
    Assert.areEqual(expected\[x\],
        example\[x\].round(System.RoundingMode.HALF\_EVEN));
}

### HALF\_UP Rounding Mode Example

Decimal\[\] example = new Decimal\[\]{5.5, 1.1, -1.1, -2.7};
Long\[\] expected = new Long\[\]{6, 1, -1, -3};
for(integer x = 0; x < example.size(); x++){
    Assert.areEqual(expected\[x\],
        example\[x\].round(System.RoundingMode.HALF\_UP));
}

### UNNECESSARY Rounding Mode Example

Decimal example1 = 5.5;
Decimal example2 = 1.0;
Assert.areEqual(1,
    example2.round(System.RoundingMode.UNNECESSARY));
try{
    example1.round(System.RoundingMode.UNNECESSARY);
} catch(Exception E) {
    Assert.areEqual('System.MathException', E.getTypeName());
}

### UP Rounding Mode Example

Decimal\[\] example = new Decimal\[\]{5.5, 1.1, -1.1, -2.7};
Long\[\] expected = new Long\[\]{6, 2, -2, -3};
for(integer x = 0; x < example.size(); x++){
    Assert.areEqual(expected\[x\],
        example\[x\].round(System.RoundingMode.UP));
}

## Decimal Methods

The following are methods for Decimal.

- **[abs()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_abs)**  
    Returns the absolute value of the Decimal.
- **[divide(divisor, scale)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_divide)**  
    Divides this Decimal by the specified divisor, and sets the scale, that is, the number of decimal places, of the result using the specified scale.
- **[divide(divisor, scale, roundingMode)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_divide_2)**  
    Divides this Decimal by the specified divisor, sets the scale, that is, the number of decimal places, of the result using the specified scale, and if necessary, rounds the value using the rounding mode.
- **[doubleValue()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_doubleValue)**  
    Returns the Double value of this Decimal.
- **[format()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_format)**  
    Returns the String value of this Decimal using the locale of the context user.
- **[intValue()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_intValue)**  
    Returns the Integer value of this Decimal.
- **[longValue()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_longValue)**  
    Returns the Long value of this Decimal.
- **[pow(exponent)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_pow)**  
    Returns the value of this decimal raised to the power of the specified exponent.
- **[precision()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_precision)**  
    Returns the total number of digits for the Decimal.
- **[round()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_round)**  
    Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using half-even rounding mode, that is, it rounds towards the “nearest neighbor” unless both neighbors are equidistant, in which case, this mode rounds towards the even neighbor.
- **[round(roundingMode)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_round_2)**  
    Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using the rounding mode specified by the rounding mode.
- **[scale()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_scale)**  
    Returns the scale of the Decimal, that is, the number of decimal places.
- **[setScale(scale)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_setScale)**  
    Returns the Decimal scaled to the specified number of decimal places, using half-even rounding, if necessary. Half-even rounding mode rounds toward the “nearest neighbor.” If both neighbors are equidistant, the number is rounded toward the even neighbor.
- **[setScale(scale, roundingMode)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_setScale_2)**  
    Returns the Decimal scaled to the specified number of decimal places, using the specified rounding mode, if necessary.
- **[stripTrailingZeros()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_stripTrailingZeros)**  
    Returns the Decimal with any trailing zeros removed.
- **[toPlainString()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_toPlainString)**  
    Returns the String value of this Decimal, without using scientific notation.
- **[valueOf(doubleToDecimal)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_valueOf)**  
    Returns a Decimal that contains the value of the specified Double.
- **[valueOf(longToDecimal)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_valueOf_2)**  
    Returns a Decimal that contains the value of the specified Long.
- **[valueOf(stringToDecimal)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_decimal.htm#apex_System_Decimal_valueOf_3)**  
    Returns a Decimal that contains the value of the specified String. As in Java, the string is interpreted as representing a signed Decimal.

### abs()

Returns the absolute value of the Decimal.

#### Signature

public Decimal abs()

#### Return Value

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

#### Example

Decimal myDecimal = -6.02214129;
Assert.areEqual(6.02214129, myDecimal.abs());

### divide(divisor, scale)

Divides this Decimal by the specified divisor, and sets the scale, that is, the number of decimal places, of the result using the specified scale.

#### Signature

public Decimal divide(Decimal divisor, Integer scale)

#### Parameters

- **divisor**: Type: [Decimal](#apex_methods_system_decimal "Contains methods for the Decimal primitive data type.")
- **scale**: Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.")

#### Return Value

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

#### Example

Decimal decimalNumber = 19;
Decimal result = decimalNumber.divide(100, 3);
Assert.areEqual(0.190, result);

### divide(divisor, scale, roundingMode)

Divides this Decimal by the specified divisor, sets the scale, that is, the number of decimal places, of the result using the specified scale, and if necessary, rounds the value using the rounding mode.

#### Signature

public Decimal divide(Decimal divisor, Integer scale, System.RoundingMode roundingMode)

#### Parameters

- **divisor**: Type: [Decimal](#apex_methods_system_decimal "Contains methods for the Decimal primitive data type.")
- **scale**: Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.")
- **roundingMode**: Type: [System.RoundingMode](#apex_decimal_rounding_mode "Rounding mode specifies the rounding behavior for numerical operations capable of discarding precision.")

#### Return Value

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

#### Example

Decimal myDecimal = 12.4567;
Decimal divDec = myDecimal.divide(7, 2, System.RoundingMode.UP);
Assert.areEqual(1.78, divDec);

### doubleValue()

Returns the Double value of this Decimal.

#### Signature

public Double doubleValue()

#### Return Value

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

#### Example

Decimal myDecimal = 6.62606957;
Double value = myDecimal.doubleValue();
Assert.areEqual(6.62606957, value);

### format()

Returns the String value of this Decimal 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.")

#### Usage

Scientific notation will be used if an exponent is needed.

#### Example

// U.S. locale
Decimal myDecimal = 12345.6789;
Assert.areEqual('12,345.679', myDecimal.format());

### intValue()

Returns the Integer value of this Decimal.

#### Signature

public Integer intValue()

#### Return Value

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

#### Example

Decimal myDecimal = 1.602176565;
Assert.areEqual(1, myDecimal.intValue());

### longValue()

Returns the Long value of this Decimal.

#### Signature

public Long longValue()

#### Return Value

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

#### Example

Decimal myDecimal = 376.730313461;
Assert.areEqual(376, myDecimal.longValue());

### pow(exponent)

Returns the value of this decimal raised to the power of the specified exponent.

#### Signature

public Decimal pow(Integer exponent)

#### Parameters

- **exponent**: Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.") The value of exponent must be between 0 and 32,767.

#### Return Value

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

#### Usage

If you use MyDecimal.pow(0), 1 is returned.

The Math.pow method does accept negative values.

#### Example

Decimal myDecimal = 4.12;
Decimal powDec = myDecimal.pow(2);
Assert.areEqual(16.9744, powDec);

### precision()

Returns the total number of digits for the Decimal.

#### Signature

public Integer precision()

#### Return Value

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

#### Example

For example, if the Decimal value was 123.45, precision returns 5. If the Decimal value is 123.123, precision returns 6.

Decimal D1 = 123.45;
Integer precision1 = D1.precision();
Assert.areEqual(5, precision1);
Decimal D2 = 123.123;
Integer precision2 = D2.precision();
Assert.areEqual(6, precision2);

### round()

Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using half-even rounding mode, that is, it rounds towards the “nearest neighbor” unless both neighbors are equidistant, in which case, this mode rounds towards the even neighbor.

#### Signature

public Long round()

#### Return Value

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

#### Usage

Note that this rounding mode statistically minimizes cumulative error when applied repeatedly over a sequence of calculations.

#### Example

Decimal D = 4.5;
Long L = D.round();
Assert.areEqual(4, L);

Decimal D1 = 5.5;
Long L1 = D1.round();
Assert.areEqual(6, L1);

Decimal D2 = 5.2;
Long L2 = D2.round();
Assert.areEqual(5, L2);

Decimal D3 = -5.7;
Long L3 = D3.round();
Assert.areEqual(-6, L3);

### round(roundingMode)

Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using the rounding mode specified by the rounding mode.

#### Signature

public Long round(System.RoundingMode roundingMode)

#### Parameters

- **roundingMode**: Type: [System.RoundingMode](#apex_decimal_rounding_mode "Rounding mode specifies the rounding behavior for numerical operations capable of discarding precision.")

#### Return Value

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

### scale()

Returns the scale of the Decimal, that is, the number of decimal places.

#### Signature

public Integer scale()

#### Return Value

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

#### Example

Decimal myDecimal = 9.27400968;
Assert.areEqual(8, myDecimal.scale());

### setScale(scale)

Returns the Decimal scaled to the specified number of decimal places, using half-even rounding, if necessary. Half-even rounding mode rounds toward the “nearest neighbor.” If both neighbors are equidistant, the number is rounded toward the even neighbor.

#### Signature

public Decimal setScale(Integer scale)

#### Parameters

- **scale**: Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.") The value of scale must be between –33 and 33. If the value of scale is negative, your unscaled value is multiplied by 10 to the power of the negation of scale. For example, after this operation, the value of d is 4\*10^3.
    
    Decimal d = 4000;
    d = d.setScale(-3);

#### Return Value

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

#### Usage

If you do not explicitly set the scale for a Decimal, the item from which the Decimal is created determines the scale.

- If the Decimal is created as part of a query, the scale is based on the scale of the field returned from the query.
- If the Decimal is created from a String, the scale is the number of characters after the decimal point of the String.
- If the Decimal is created from a non-decimal number, the number is first converted to a String. The scale is then set using the number of characters after the decimal point.

#### Example

Decimal myDecimal = 8.987551787;
Decimal setScaled = myDecimal.setscale(3);
Assert.areEqual(8.988, setScaled);

### setScale(scale, roundingMode)

Returns the Decimal scaled to the specified number of decimal places, using the specified rounding mode, if necessary.

#### Signature

public Decimal setScale(Integer scale, System.RoundingMode roundingMode)

#### Parameters

- **scale**: Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.")
- **roundingMode**: Type: [System.RoundingMode](#apex_decimal_rounding_mode "Rounding mode specifies the rounding behavior for numerical operations capable of discarding precision.")

#### Return Value

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

#### Usage

If you do not explicitly set the scale for a Decimal, the item from which the Decimal is created determines the scale.

- If the Decimal is created as part of a query, the scale is based on the scale of the field returned from the query.
- If the Decimal is created from a String, the scale is the number of characters after the decimal point of the String.
- If the Decimal is created from a non-decimal number, the number is first converted to a String. The scale is then set using the number of characters after the decimal point.

### stripTrailingZeros()

Returns the Decimal with any trailing zeros removed.

#### Signature

public Decimal stripTrailingZeros()

#### Return Value

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

#### Example

Decimal myDecimal = 1.10000;
Decimal stripped = myDecimal.stripTrailingZeros();
Assert.areEqual(1.1, stripped);

### toPlainString()

Returns the String value of this Decimal, without using scientific notation.

#### Signature

public String toPlainString()

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

Decimal myDecimal = 12345.6789;
Assert.areEqual('12345.6789', myDecimal.toPlainString());

### valueOf(doubleToDecimal)

Returns a Decimal that contains the value of the specified Double.

#### Signature

public static Decimal valueOf(Double doubleToDecimal)

#### Parameters

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

#### Return Value

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

#### Example

Double myDouble = 2.718281828459045;
Decimal myDecimal = Decimal.valueOf(myDouble);
Assert.areEqual(2.718281828459045, myDecimal);

### valueOf(longToDecimal)

Returns a Decimal that contains the value of the specified Long.

#### Signature

public static Decimal valueOf(Long longToDecimal)

#### Parameters

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

#### Return Value

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

#### Example

Long myLong = 299792458;
Decimal myDecimal = Decimal.valueOf(myLong);
Assert.areEqual(299792458, myDecimal);

### valueOf(stringToDecimal)

Returns a Decimal that contains the value of the specified String. As in Java, the string is interpreted as representing a signed Decimal.

#### Signature

public static Decimal valueOf(String stringToDecimal)

#### Parameters

- **stringToDecimal**: 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: [Decimal](#apex_methods_system_decimal "Contains methods for the Decimal primitive data type.")

#### Example

String temp = '12.4567';
Decimal myDecimal = Decimal.valueOf(temp);