Math Class in Apex
In Apex, the Math class is a built-in system class that provides a variety of static methods to perform mathematical operations, just like in C#. These methods are useful for calculations, rounding, power, min/max comparison, square roots, and more.
The Math class in Apex is static, meaning you don’t need to create an object to use it — just call Math.methodName() directly.
Key Features of the Apex Math Class:
1. Static Utility Class: All methods in the Math class are static, which means you can call them directly using Math. prefi
2. Common Mathematical Operations: Includes rounding, absolute values, power calculations, square roots, etc.
3. Trigonometric & Logarithmic Support: Also supports more complex operations like sine, cosine, log, etc.
Method | Description |
---|---|
Math.abs() | Returns absolute (positive) value |
Math.floor() | Rounds number down to nearest integer |
Math.round() | Rounds number to the nearest integer |
Math.max(a, b) | Returns the larger of two numbers |
Math.min(a, b) | Returns the smaller of two numbers |
Math.sqrt() | Returns square root of a number |
Math.mod(x, y) | Returns x raised to the power y |
Math.mod(x, y) | Returns remainder of x/y (modulus) |
1. Static Utility Class: All methods in the Math class are static, which means you can call them directly using Math. prefi
2. Common Mathematical Operations: Includes rounding, absolute values, power calculations, square roots, etc.
3. Trigonometric & Logarithmic Support: Also supports more complex operations like sine, cosine, log, etc.
Apex Code Examples for Common Math Methods
1. Math.floor(): Round down a decimal
apex
Double weight = 10.75;
Double floorWeight = Math.floor(weight);
System.debug(floorWeight); // Output: 10.0
2. Math.max(): Get larger of two values
Apex
Integer maxVal = Math.max(40, 60);
System.debug(maxVal); // Output: 60
3. Math.min(): Get smaller of two values
Apex
Integer minVal = Math.min(40, 60);
System.debug(minVal); // Output: 40
4. Math.sqrt(): Get square root
Apex
Double root = Math.sqrt(64);
System.debug(root); // Output: 8.0
5. Math.abs(): Get absolute value
Apex
Double temp = Math.abs(-5.2);
System.debug(temp); // Output: 5.2
6. Math.round(): Round to nearest integer
apex
Double height = Math.round(7.6);
System.debug(height); // Output: 8.0
7. Math.pow(): Power calculation
apex
Double result = Math.pow(2.0, 3.0); // 2^3 = 8
System.debug(result); // Output: 8.0
8. Truncate a decimal (Workaround)
Apex
Double amount = 100000.897;
Integer truncatedAmount = (Integer)amount;
System.debug(truncatedAmount); // Output: 100000
Practice Tasks (Apex)
Write programs using the Math class methods in Apex:
1. Round the number 9.3 to the nearest integer using Math.round().
2. Round the number 12.89 down using Math.floor().
3. Find the absolute value of -100.50 using Math.abs().
4. Calculate the square root of 225 using Math.sqrt().
5. Find the maximum between 77 and 99 using Math.max().
6. Find the minimum between 15 and 25 using Math.min().
7. Remove the decimal part of 98765.4321 using type casting.
8. Calculate 3 raised to the power 4 using Math.pow().
9. Perform a combination of these:
• Round 5.8
• Get absolute of -10
• Square root of 144
• Max of 10 and 20
• Min of 5 and 15
• Truncate 9876.54321