web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Divide by 0 error

(0) ShareShare
ReportReport
Posted on by 240

 Hi everyone

I am new to programming and learning slowly.  I have added some code to calculate monthly payment, but I am getting a divide by 0 error even though I am not dividing by 0.

How can I resolve this.

 My code:

  if (FinancingRateTable.FinancingRate == 0.00)
    {
        
         return (element.dspAmtFinance()/(36)); <--When month is hardcoded No error is present
       
         //return (element.dspAmtfinance()/(FinancingTermTable.Months)); <-- this way, I get error
    
     }
 

when I return 'FinancingTermTable.Months' i get my terms in month, what I dont understand is how I get a divide by 0 error, please enlighten me on this...

 

 

Thanks, 

 


G


 

*This post is locked for comments

I have the same question (0)
  • Oladipupo Onibile Profile Picture
    252 on at
    Re: Divide by 0 error
    FinancingRateTable is an instance of what class. Your code is incomplete
  • Girac Bajac Profile Picture
    240 on at
    Re: Re: Divide by 0 error

     Like I said, I am new to programming so please bear with me,  and only included a snipplet in my orig posting.

    When i return the 'FinancingTermTable.Months' i get the correct number. When I return the 'element.DspAmtFinance' I get the correct figure. when I combine them I get a Divide by 0 in an infobox, when I acknowledge it, I get the correct value. So, it seems to me that the calculation is being preformed correctly.

     

     

    Here is the entire code of the Method:

    // BP Deviation documented
    display salesPrice dspMonthlyPmt()
    {
        FinancingRateTable FinancingRateTable;
        FinancingTermTable FinancingTermTable;
        FinancingTable     financingTable =  FinancingTable::findProjInvoice( QuotationLine.ProjInvoiceProjId);
        ;
        FinancingRateTable =  FinancingRateTable::find(financingTable.FinancingId,  QuotationLine.FinancingRateId);
        FinancingTermTable =  FinancingTermTable::find(financingTable.FinancingId,  QuotationLine.FinancingTermId);

    //Add logic for 0 percent financing
         if (FinancingRateTable.FinancingRate == 0.00)
        {
               return (element.DspAmtFinance()/(36));
             //return (FinancingTermTable.Months)/100;
             //return (element.DspAmtFinance()/(FinancingTermTable.Months));
            // return FinancingRateTable.FinancingRate;
         }

    //End logic for 0 percent financing
        else if (element.DspAmtFinance() && FinancingRateTable.FinancingRate && FinancingTermTable.Months)
        {
            return pmt(element.DspAmtFinance(), (FinancingRateTable.FinancingRate/100 * (FinancingTermTable.Months/12) / FinancingTermTable.Months), FinancingTermTable.Months);
        }
        else
        {

          // return pmt(element.DspAmtFinance()/(FinancingTermTable.Months));
            return element.DspAmtFinance();
        }
    }

  • Oladipupo Onibile Profile Picture
    252 on at
    Re: Re: Re: Divide by 0 error
    Replace your code with this
    return (num2str(element.dspAmtfinance()/(FinancingTermTable.Months),10,2,1,1));

    So your code looks like this

    if (FinancingRateTable.FinancingRate == 0.00)
    {
    return (element.dspAmtFinance()/(36)); <--When month is hardcoded No error is present
    //when you divide an int with another int, you get a rounded up figure i.e 0.8=0
    //So instead, you save your result to a datatype real
    //num2str() recieves a real value and converts to a string value

    return (num2str(element.dspAmtfinance()/(FinancingTermTable.Months),10,2,1,1));
    }
    I hope this solves d problem
  • Girac Bajac Profile Picture
    240 on at
    Re: Re: Re: Re: Divide by 0 error

     Thank you for explaining this, it makes sense. However I now receive "The operand is not compatable with this type of function"

    When I did a simple "return (Num2Str(12345.6,10,2,1,1));" I also received the error when compiling. I also tried "(Num2Str(12345.6,10,2,1,1));" No complie errors  but noithing will get returned.

  • Oladipupo Onibile Profile Picture
    252 on at
    Re: Re: Re: Re: Re: Divide by 0 error
    The reason is obvious. When you use
    return (Num2Str(12345.6,10,2,1,1));
    Previously, the guy calling the function is expecting your function to return an integer value, and not a string value (sorry I didnt put that into consideration when helpn u). try converting back to int using str2int() bug again remember that str2int() would round up your result. str2Num() would not round up as it would return a real value but again remember that the guy calling your function is expecting an Integer value.
  • Manish Menon Profile Picture
    425 on at
    Re: Re: Re: Re: Re: Divide by 0 error

    [quote user="Girac Bajac"]

     Thank you for explaining this, it makes sense. However I now receive "The operand is not compatable with this type of function"

    When I did a simple "return (Num2Str(12345.6,10,2,1,1));" I also received the error when compiling. I also tried "(Num2Str(12345.6,10,2,1,1));" No complie errors  but noithing will get returned.

    [/quote]

    This error is coming may be because your display method have the return type of Salesprice i think!! make it to Real!! you can resolve this error

  • Girac Bajac Profile Picture
    240 on at
    Re: Re: Re: Re: Re: Re: Divide by 0 error

     It works now, by using 'real'

    thank you all for your assistance!

    G

  • Rajdip Das Profile Picture
    3,333 on at
    Re: Divide by 0 error

     Hi,

    If your  field "FinancingTermTable.Months" is Integer/Real DataType then do the following....

     if (FinancingRateTable.FinancingRate == 0.00)
     {
          if(FinancingTermTable.Months)
              return (element.dspAmtfinance()/(FinancingTermTable.Months));
          return 0;
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
AlissonGodoy Profile Picture

AlissonGodoy 2

#2
Community Member Profile Picture

Community Member 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans