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
Hardik BHardik B 

Error: Calculating Compound Interest on Monthly basis

Hello All,

I've a situation when calculation compound interest on monthly basis. I'm using the formula provided by the Salesforce for Compound Interest but it throws me an error. Here is the execute anon code snippet I'm trying first to figure out if I'm getting the proper output or not:

Integer mic= 50000;
Decimal int_rate= 9.5;
Integer tenure= 4;

Decimal emi= mic*(1+int_rate/12)^(tenure*12);

system.debug('Compound Interest=' +emi.round());

This is Error I'm getting: Line: 5, Column: 14
^ operator can only be applied to Boolean expressions or to Integer or Long expressions

I want to calculate montly emi so I'm using the compound interest for a specific tenure of period.

Why am I seeing this error I dont know can anyone please help me out with this ?


Help will be really appreciated.

Regards,
Hardik B.

 
Suraj TripathiSuraj Tripathi

Hi Hardik,
Please try this piece of code. Hope it will help you:
 

public  class questest {
    public static void test(){
 decimal mic= 50000;
 double int_rate= 9.5;
 double tenure= 4;
        
 Decimal emi= mic * (math.pow((1+int_rate/4),(tenure*12)));
    //  Decimal emi= mic*(1+int_rate/12)^(tenure*12);
     system.debug('Compound Interest=' +emi.round());
   }
}

Regards,
Suraj​
Hardik BHardik B
Hi Suraj,

I tried this code in exe anon but the output is negative and as expected output. Any more tweaks need to b done ?
Suraj TripathiSuraj Tripathi

Hi Hardik,

Please exchange the line with. 

Decimal emi= mic * (math.pow((1+int_rate/12),(tenure*12)));
Hardik BHardik B
It still shows the negative value and the output is wat too much more than expected. It shows somewhere around 15 to 16 digits without decimal. There is something wrong with the calculation.

 
Alain CabonAlain Cabon
Hi,

Be careful, it is a percent rate (%) so double int_rate= 9.5/100;

The initial misunderstanding:

Formula: ^ (Exponentiation) Raises a number to a power of a specified number.
Apex:  x ^ y Bitwise exclusive OR operator.

http:// https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm (http:// https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm)
https://help.salesforce.com/articleView?id=customize_functions.htm&type=0
Suraj TripathiSuraj Tripathi

Hi Hardik,
Please try this piece of code. Hope it will help you:
 

public class questest {
    public static void test(){
        Decimal mic= 50000.00;
        Double int_rate= 9.5;
        Double tenure = 4;
 
        Double d1 = math.pow((1+((int_rate/100)/12)),tenure*12);
        String s1 = String.valueOf(d1);
        String[] s2 = s1.split('E');
        
        Decimal  Total_Amt_With_Interest = mic * (decimal.valueOf(s2[0]));
        System.debug('Total Amoount With Interest = '+Total_Amt_With_Interest.round());
        
        decimal Monthly_Emi =  Total_Amt_With_Interest/(tenure*12);
        System.debug('Month Emi = '+Monthly_Emi.round()); 
    }      
}
If this code helps you, Please mark it as best.