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
MaheemSamMaheemSam 

Math Operation is returning a wrong result

Hi, 

  I am using a below formula  Here Resller Discount = 25% and Margin = 7%
Disti discount% =  Margin% * (1-Reseller dicount%) + Reseller discount%
 If I use above formula in excel it is giving Disti Discount= 30.35%

 I am using the same formula in salesforce Apex but Disti Discount  is returning as 25% it self Please suggest me what might be the mistake in this math opeation I am doing
public PageReference addDistiDisc() {
        Decimal Margin = 7/100;
        Decimal Ddisc;
        Decimal Rdisc;
       for(QuoteLineWrapper qw : lines) {
         Rdisc = Decimal.Valueof(qw.qline.Reseller_Discount__c)/100;
         Ddisc = Margin  * (1 - Rdisc  ) + Rdisc ;
         qw.qline.Disti_Discount__c =  String.Valueof( (Ddisc) * 100);
         updateTotal(qw);
         qw.isChanged = true; 
        } 
      return null;
    }

Thanks
Sudhir
 
Best Answer chosen by MaheemSam
sfdcMonkey.comsfdcMonkey.com
use below code :
Decimal Margin = 7;
        Decimal Ddisc = 25;
        Decimal Rdisc = 25;
        Decimal DMargin;
        Decimal RMargin;

        DMargin = ( Margin  * (1 - Rdisc / 100) )  + Rdisc ;

        RMargin = (Ddisc - Margin) / (1 - Margin /100);

        System.debug('Disti Discount Margin = '  + DMargin);

  	System.debug('Reseller Discount Margin = '  + RMargin);
User-added image
Thanks
let me inform if it helps you

 

All Answers

MaheemSamMaheemSam
Hi Piyush, 

   I tried your formula it is giving different result 31.75 I am using below formula which is returning   Disti Discount Margin = 46.23% and Reseller Discount Margin = 50%
 
Decimal Margin = 7;
        Decimal Ddisc = 25;
        Decimal Rdisc = 25;
        Decimal DMargin;
        Decimal RMargin;

        DMargin = ( Margin  * (1 - Rdisc) )  + Rdisc ;

        RMargin = (Ddisc - Margin) / (1 - Margin);

        System.debug('Disti Discount Margin = '  + DMargin);

  	System.debug('Reseller Discount Margin = '  + DMargin);

  In apex it is returning as -143 which is different Please suggest. 


Thanks
Sudhir


 
MaheemSamMaheemSam
Thanks Piush,  System.debug was priinting same value so it is coming same result you have fixed the DMargin value but RMaring is not comming correclty 

  When I enter 25 for Ddisc RMargin should come as 19.35 instead it is returning -3 Please suggest
        Decimal Margin = 7;
        Decimal Ddisc = 25;
        Decimal Rdisc = 25;
        Decimal DMargin;
        Decimal RMargin;

        DMargin = ( Margin  * (1 - Rdisc / 100) )  + Rdisc ;

        RMargin = (Ddisc - Margin) / (1 - Margin);

        System.debug('Disti Discount Margin = '  + DMargin);

  	 System.debug('Reseller Discount Margin = '  + RMargin);

Thanks
Sudhir
sfdcMonkey.comsfdcMonkey.com
use below code :
Decimal Margin = 7;
        Decimal Ddisc = 25;
        Decimal Rdisc = 25;
        Decimal DMargin;
        Decimal RMargin;

        DMargin = ( Margin  * (1 - Rdisc / 100) )  + Rdisc ;

        RMargin = (Ddisc - Margin) / (1 - Margin /100);

        System.debug('Disti Discount Margin = '  + DMargin);

  	System.debug('Reseller Discount Margin = '  + RMargin);
User-added image
Thanks
let me inform if it helps you

 
This was selected as the best answer
MaheemSamMaheemSam
You resolved this issue Thanks Piyush