For more information on Decimal, see Primitive Data Types.
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.
The following are methods for Decimal.
public Decimal abs()
Type: Decimal
Decimal myDecimal = -6.02214129;
System.assertEquals(6.02214129, myDecimal.abs());
public Decimal divide(Decimal divisor, Integer scale)
Type: Decimal
Decimal decimalNumber = 19; Decimal result = decimalNumber.divide(100, 3); System.assertEquals(0.190, result);
public Decimal divide(Decimal divisor, Integer scale, System.RoundingMode roundingMode)
Type: Decimal
Decimal myDecimal = 12.4567; Decimal divDec = myDecimal.divide(7, 2, System.RoundingMode.UP); System.assertEquals(divDec, 1.78);
public Double doubleValue()
Type: Double
Decimal myDecimal = 6.62606957; Double value = myDecimal.doubleValue(); System.assertEquals(6.62606957, value);
public String format()
Type: String
Scientific notation will be used if an exponent is needed.
// U.S. locale Decimal myDecimal = 12345.6789; system.assertEquals('12,345.679', myDecimal.format());
public Integer intValue()
Type: Integer
Decimal myDecimal = 1.602176565;
system.assertEquals(1, myDecimal.intValue());
public Long longValue()
Type: Long
Decimal myDecimal = 376.730313461;
system.assertEquals(376, myDecimal.longValue());
public Decimal pow(Integer exponent)
Type: Decimal
If you use MyDecimal.pow(0), 1 is returned.
The Math.pow method does accept negative values.
Decimal myDecimal = 4.12; Decimal powDec = myDecimal.pow(2); System.assertEquals(powDec, 16.9744);
public Integer precision()
Type: Integer
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(); system.assertEquals(precision1, 5); Decimal D2 = 123.123; Integer precision2 = D2.precision(); system.assertEquals(precision2, 6);
public Long round()
Type: Long
Note that this rounding mode statistically minimizes cumulative error when applied repeatedly over a sequence of calculations.
Decimal D = 4.5; Long L = D.round(); System.assertEquals(4, L); Decimal D1 = 5.5; Long L1 = D1.round(); System.assertEquals(6, L1); Decimal D2 = 5.2; Long L2 = D2.round(); System.assertEquals(5, L2); Decimal D3 = -5.7; Long L3 = D3.round(); System.assertEquals(-6, L3);
public Long round(System.RoundingMode roundingMode)
Type: Long
public Integer scale()
Type: Integer
Decimal myDecimal = 9.27400968;
system.assertEquals(8, myDecimal.scale());
public Decimal setScale(Integer scale)
Decimal d = 4000;
d = d.setScale(-3);
Type: Decimal
Decimal myDecimal = 8.987551787; Decimal setScaled = myDecimal.setscale(3); System.assertEquals(8.988, setScaled);
public Decimal setScale(Integer scale, System.RoundingMode roundingMode)
Decimal d = 4000;
d = d.setScale(-3);
Type: Decimal
public Decimal stripTrailingZeros()
Type: Decimal
Decimal myDecimal = 1.10000; Decimal stripped = myDecimal.stripTrailingZeros(); System.assertEquals(stripped, 1.1);
public String toPlainString()
Type: String
Decimal myDecimal = 12345.6789; System.assertEquals('12345.6789', myDecimal.toPlainString());
public static Decimal valueOf(Double doubleToDecimal)
Type: Decimal
Double myDouble = 2.718281828459045; Decimal myDecimal = Decimal.valueOf(myDouble); System.assertEquals(2.718281828459045, myDecimal);
public static Decimal valueOf(Long longToDecimal)
Type: Decimal
Long myLong = 299792458; Decimal myDecimal = Decimal.valueOf(myLong); System.assertEquals(299792458, myDecimal);
public static Decimal valueOf(String stringToDecimal)
Type: Decimal
String temp = '12.4567'; Decimal myDecimal = Decimal.valueOf(temp);