No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Decimal Class
Namespace
Usage
For more information on Decimal, see Primitive Data Types.
Rounding Mode
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.
Decimal Methods
The following are methods for Decimal.
abs()
Signature
public Decimal abs()
Return Value
Type: Decimal
divide(Decimal, Integer)
Signature
public Decimal divide(Decimal divisor, Integer scale)
Return Value
Type: Decimal
Example
In the following example, D has the value of 0.190.
1Decimal D = 19;
2
3D.Divide(100, 3);divide(Decimal, Integer, Object)
Signature
public Decimal divide(Decimal divisor, Integer scale, Object roundingMode)
Parameters
- divisor
- Type: Decimal
- scale
- Type: Integer
- roundingMode
- Type: System.RoundingMode
Return Value
Type: Decimal
Example
1Decimal myDecimal = 12.4567;
2
3Decimal divDec = myDecimal.divide
4
5 (7, 2, System.RoundingMode.UP);
6
7system.assertEquals(divDec, 1.78);doubleValue()
Signature
public Double doubleValue()
Return Value
Type: Double
format()
Signature
public String format()
Return Value
Type: String
Usage
Scientific notation will be used if an exponent is needed.
intValue()
Signature
public Integer intValue()
Return Value
Type: Integer
pow(Integer)
Signature
public Decimal pow(Integer exponent)
Parameters
- exponent
- Type: Integer
- The value of exponent must be between 0 and 32,767.
Return Value
Type: Decimal
Usage
If you use MyDecimal.pow(0), 1 is returned.
The Math.pow method does accept negative values.
Example
1Decimal myDecimal = 4.12;
2
3Decimal powDec = myDecimal.pow(2);
4
5system.assertEquals(powDec, 16.9744);precision()
Signature
public Integer precision()
Return Value
Type: Integer
Example
For example, if the Decimal value was 123.45, precision returns 5. If the Decimal value is 123.123, precision returns 6.
1Decimal D1 = 123.45;
2Integer precision1 = D1.precision();
3system.assertEquals(precision1, 5);
4Decimal D2 = 123.123;
5Integer precision2 = D2.precision();
6system.assertEquals(precision2, 6);round()
Signature
public Long round()
Return Value
Type: Long
Usage
Note that this rounding mode statistically minimizes cumulative error when applied repeatedly over a sequence of calculations.
Example
1Decimal D = 4.5;
2Long L = D.round();
3System.assertEquals(4, L);
4
5Decimal D1 = 5.5;
6Long L1 = D1.round();
7System.assertEquals(6, L1);
8
9Decimal D2 = 5.2;
10Long L2 = D2.round();
11System.assertEquals(5, L2);
12
13Decimal D3 = -5.7;
14Long L3 = D3.round();
15System.assertEquals(-6, L3);round(System.RoundingMode)
Signature
public Long round(System.RoundingMode roundingMode)
Parameters
- roundingMode
- Type: System.RoundingMode
Return Value
Type: Long
scale()
Signature
public Integer scale()
Return Value
Type: Integer
setScale(Integer)
Signature
public Decimal setScale(Integer scale)
Parameters
- scale
- Type: Integer
- The value of scale must be between –33 and 33.
Return Value
Type: Decimal
Usage
- 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 scale is determined by converting the number to a String and then using the number of characters after the decimal point.
setScale(Integer, System.RoundingMode)
Signature
public Decimal setScale(Integer scale, System.RoundingMode roundingMode)
Parameters
- scale
- Type: Integer
- The value of scale must be between -32,768 and 32,767.
- roundingMode
- Type: System.RoundingMode
Return Value
Type: Decimal
Usage
- 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 scale is determined by converting the number to a String and then using the number of characters after the decimal point.
stripTrailingZeros()
Signature
public Decimal stripTrailingZeros()
Return Value
Type: Decimal
toPlainString()
Signature
public String toPlainString()
Return Value
Type: String
valueOf(String)
Signature
public static Decimal valueOf(String convertToDecimal)
Parameters
- convertToDecimal
- Type: String
Return Value
Type: Decimal
Example
1String temp = '12.4567';
2
3Decimal myDecimal = decimal.valueOf(temp);