Newer Version Available
Decimal Class
Namespace
Usage
For more information on Decimal, see Decimal Data Type.
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
Example
divide(divisor, scale, roundingMode)
Signature
public Decimal divide(Decimal divisor, Integer scale, System.RoundingMode roundingMode)
Parameters
- divisor
- Type: Decimal
- scale
- Type: Integer
- roundingMode
- Type: System.RoundingMode
Return Value
Type: Decimal
Example
doubleValue()
Signature
public Double doubleValue()
Return Value
Type: Double
Example
format()
Signature
public String format()
Return Value
Type: String
Usage
Scientific notation will be used if an exponent is needed.
Example
intValue()
Signature
public Integer intValue()
Return Value
Type: Integer
Example
longValue()
Signature
public Long longValue()
Return Value
Type: Long
Example
pow(exponent)
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
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.
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
round(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
Example
setScale(scale)
Signature
public Decimal setScale(Integer scale)
Parameters
- scale
- Type: Integer
-
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.
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 number is first converted to a String. Scale is then set using the number of characters after the decimal point.
Example
setScale(scale, roundingMode)
Signature
public Decimal setScale(Integer scale, System.RoundingMode roundingMode)
Parameters
- scale
- Type: Integer
- 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.
- 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 number is first converted to a String. Scale is then set using the number of characters after the decimal point.
stripTrailingZeros()
Signature
public Decimal stripTrailingZeros()
Return Value
Type: Decimal
Example
toPlainString()
Signature
public String toPlainString()
Return Value
Type: String