No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Math Class
Contains methods for mathematical operations.
Namespace
Math Methods
The following are methods for Math. All methods are static.
mod(Integer, Integer)
Returns the remainder of i1 divided
by i2.
Signature
public static Integer mod(Integer i1, Integer i2)
Return Value
Type: Integer
Example
1Integer remainder = math.mod(12, 2);
2system.assertEquals(remainder, 0);
3
4Integer remainder2 = math.mod(8, 3);
5system.assertEquals(remainder2, 2);random()
Returns a positive Double that is greater than or equal
to 0.0 and less than 1.0.
Signature
public static Double random()
Return Value
Type: Double
round(Double)
Do not use. This method is deprecated as of the Winter
'08 release. Instead, use Math.roundToLong. Returns the closest Integer to the specified Double. If the result
is less than -2,147,483,648 or greater than 2,147,483,647, Apex generates an error.
Signature
public static Integer round(Double d)
Parameters
- d
- Type: Double
Return Value
Type: Integer
round(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.
Signature
public static Integer round(Decimal d)
Parameters
- d
- Type: Decimal
Return Value
Type: Integer
Usage
Note that this rounding mode statistically minimizes cumulative error when applied repeatedly over a sequence of calculations.
Example
1Decimal d1 = 4.5;
2Integer i1 = Math.round(d1);
3System.assertEquals(4, i1);
4
5Decimal d2 = 5.5;
6Integer i2 = Math.round(d2);
7System.assertEquals(6, i2);roundToLong(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.
Signature
public static Long roundToLong(Decimal d)
Parameters
- d
- Type: Decimal
Return Value
Type: Long
Usage
Note that this rounding mode statistically minimizes cumulative error when applied repeatedly over a sequence of calculations.
Example
1Decimal d1 = 4.5;
2Long i1 = Math.roundToLong(d1);
3System.assertEquals(4, i1);
4
5Decimal d2 = 5.5;
6Long i2 = Math.roundToLong(d2);
7System.assertEquals(6, i2);tanh(Decimal)
Returns the hyperbolic tangent of d.
The hyperbolic tangent of d is defined to be (ex - e-x)/(ex + e-x) where e is Euler's number. In other words,
it is equivalent to sinh(x)/cosinh(x). The absolute value of the exact tanh is always less than 1.
Signature
public static Decimal tanh(Decimal d)
Parameters
- d
- Type: Decimal
Return Value
Type: Decimal
tanh(Double)
Returns the hyperbolic tangent of d.
The hyperbolic tangent of d is defined to be (ex - e-x)/(ex + e-x) where e is Euler's number. In other words,
it is equivalent to sinh(x)/cosinh(x). The absolute value of the exact tanh is always less than 1.
Signature
public static Double tanh(Double d)
Parameters
- d
- Type: Double
Return Value
Type: Double