function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jayamjayam 

modulus operator in apex classes

 I tried with mod(integer,integer) and  integer %10 but errors are thrown

what can i do to get remainder when division operation is performed

Ispita_NavatarIspita_Navatar

Hi,

I don't think apex has a modulus operator , hence the error.

 

Try going though the documentation in the following link page 32- 37 :-

 

http://wiki.developerforce.com/images/3/3e/Salesforce_apex_language_reference.pdf

 

Hope this helps.

shra1_devshra1_dev

hi jayam,

 

to perform modular operation between two numbers say 'a' and 'b'

 

try using 

 

sum = math.mod(a,b);

 

//system.debug(sum);

TRossiTRossi

// 100 % 97

Integer reminder = math.mod(100, 97);

// reminder = 3

 

yes this works fine thanks:)

Mohd AliyanMohd Aliyan
Hello dear,

% operator doesnot exist in salesforce but we can access this using math class like:

result = math.mod(x,y);
you can use anywhere like :


        integer x=21,y;
        y=math.mod(x,5);
        system.debug('Remainder is :'+y); // Result is 1
 
Wagner CoelhoWagner Coelho
hi dear, 

resto = Math.mod(Integer.valueOf(quantidade * 100000), Integer.valueOf(multiplo * 100000));
Amit Bhagat 2Amit Bhagat 2
Integer remainder = math.mod(12, 2);
System.debug(remainder);