1

Does anyone know how to implement the excel CUMIPMT function in javascript. I found this code but it seems to be missing some functions.

1 Answer 1

1

This is pseudocode, you will need to adjust it:

Object CUMIPMT(double rate, double nper, double pv, double start_period, double end_period, double type)
        {

            if (rate <= 0)
            {
                return "#NUM!";
            }

            if (nper <= 0)
            {
                return "#NUM!";
            }
            nper = (int)nper;

            if (pv <= 0)
            {
                return "#NUM!";
            }

            if (start_period < 1)
            {
                return "#NUM!";
            }
            start_period = (int)start_period;

            if (end_period < 1)
            {
                return "#NUM!";
            }
            end_period = (int)end_period;

            if (start_period > end_period)
            {
                return "#NUM!";
            }

            if (!(type == 0 || type == 1))
            {
                return "#NUM!";
            }

            double value = 0;
            for (int per = (int)start_period; per <= (int)end_period; per++)
            {
                value += IPMT(rate, nper, per, pv, 0, type);
            }

            return value;
        }

        double IPMT(double rate, double nper, double period, double pv, double fv, double type)
        {
            if (rate == 0)
            {
                return 0;
            }
            if (type == 1 && period == 1)
            {
                return 0;
            }
            double pmt = PMT(rate, nper, pv, fv, 0);
            if (type == 0)
            {
                return (pv * Math.Pow(1.0 + rate, period - 1) + pmt * ((Math.Pow(1 + rate, period - 1) - 1) / rate)) * rate * (-1.0);
            }
            else
            {
                double pmt2 = PMT(rate, nper, pv, fv, 1);
                return pmt2 - pmt + (pv * Math.Pow(1.0 + rate, period - 2) + pmt * ((Math.Pow(1 + rate, period - 2) - 1) / rate)) * rate * (-1.0);
            }

        }

        double PMT(double rate, double nper, double pv, double fv, double type)
        {
            if (rate == 0)
            {
                return (-1.0) * (fv + pv) / nper;
            }
            else
            {
                return (-1.0) *
                        ((fv + pv * Math.Pow((1 + rate), nper)) *
                        rate /
                        ((1 + rate * type) * (Math.Pow((1 + rate), nper) - 1))
                    );
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks..This is what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.