Math Class use in Apex #inSalesforce
This class contains methods for mathematical operations.
Examples:
Integer i = -42;
Integer i2 = math.abs(i);
system.assertEquals(i2, 42);
Integer i2 = math.abs(i);
system.assertEquals(i2, 42);
Decimal larger = math.max(12.3, 156.6);
system.assertEquals(larger, 156.6);
Decimal smaller = math.min(12.3, 156.6);
system.assertEquals(smaller, 12.3);
system.assertEquals(smaller, 12.3);
Integer remainder = math.mod(12, 2);
system.assertEquals(remainder, 0);
system.assertEquals(remainder, 0);
Integer remainder2 = math.mod(8, 3);
system.assertEquals(remainder2, 2);
system.assertEquals(remainder2, 2);
Decimal d1 = 4.5;
Integer i1 = Math.round(d1);
System.assertEquals(4, i1);
Integer i1 = Math.round(d1);
System.assertEquals(4, i1);
Decimal d2 = 5.5;
Integer i2 = Math.round(d2);
System.assertEquals(6, i2);
Integer i2 = Math.round(d2);
System.assertEquals(6, i2);
Decimal d1 = 4.5;
Long i1 = Math.roundToLong(d1);
System.assertEquals(4, i1);
Long i1 = Math.roundToLong(d1);
System.assertEquals(4, i1);
Decimal d2 = 5.5;
Long i2 = Math.roundToLong(d2);
System.assertEquals(6, i2);
Long i2 = Math.roundToLong(d2);
System.assertEquals(6, i2);
Visual: https://www.youtube.com/watch?v=7mS3NtvleCM
Reference: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_math.htm
Comments
Post a Comment