Thursday, July 3, 2014

Javascript Function to calculate present value of an investment.

// Function to calculate present value of an investment..

// Parameters are rate, total number of periods, payment made each period, future value and type (when payments are due)

function pv(rate, per, nper, pmt, fv) {

    nper = parseFloat(nper);

    pmt = parseFloat(pmt);

    fv = parseFloat(fv);

    rate = eval((rate) / (per * 100));

    if (nper == 0) {
        //(pmt == 0) ||
        alert("Why do you want to test me with zeros?");

        return (0);

    }

    if (rate == 0) // Interest rate is 0
    {

        pv_value = -(fv + (pmt * nper));

    }

    else {

        x = Math.pow(1 + rate, -nper);

        y = Math.pow(1 + rate, nper);

        pv_value = -(x * (fv * rate - pmt + y * pmt)) / rate;

    }

    pv_value = conv_number(pv_value, 2);

    return (pv_value);

}

function conv_number(expr, decplaces) { // This function is from David Goodman's Javascript Bible.

    var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));

    while (str.length <= decplaces) {

        str = "0" + str;

    }

    var decpoint = str.length - decplaces;

    return (str.substring(0, decpoint) + "." + str.substring(decpoint, str.length));

}
//end of pv

Javascript Function to calculate future value of an investment..

// Function to calculate future value of an investment..

function fv(rate, per, nper, pmt, pv) {

    nper = parseFloat(nper);

    pmt = parseFloat(pmt);

    pv = parseFloat(pv);

    rate = eval((rate) / (per * 100));

    if ((nper == 0)) {
        //(pmt == 0) ||
        alert("Why do you want to test me with zeros?");

        return (0);

    }

    if (rate == 0) // Interest rate is 0
    {

        fv_value = -(pv + (pmt * nper));

    }

    else {

        x = Math.pow(1 + rate, nper);

        // y = Math.pow(1 + rate, nper);

        fv_value = -(-pmt + x * pmt + rate * x * pv) / rate;

    }

    fv_value = conv_number(fv_value, 2);

    return (fv_value);

}


//end of future value

Javascript Function to calculate Periodic Payments(PMT)

// Function to calculate Periodic Payments.

function pmt(rate, per, nper, pv, fv) {

    fv = parseFloat(fv);

    nper = parseFloat(nper);

    pv = parseFloat(pv);

    per = parseFloat(per);

    if ((per == 0) || (nper == 0)) {

        alert("Why do you want to test me with zeros?");

        return (0);

    }

    rate = eval((rate) / (per * 100));

    if (rate == 0) // Interest rate is 0
    {

        pmt_value = -(fv + pv) / nper;

    }

    else {

        x = Math.pow(1 + rate, nper);

        pmt_value = -((rate * (fv + x * pv)) / (-1 + x));

    }

    pmt_value = conv_number(pmt_value, 2);

    return (pmt_value);

}

//end Of PMT

Javascript Function To calculate Effective RATE

//Function To calculate Effective RATE

function RATE(annualrate, nper) {
    var rate;
    annualrate = 1 + (annualrate / 100);
    rate = parseFloat(Math.pow(annualrate, 1 / nper) - 1)
    return (rate);
}

Javascript Function To Calculate Time To Save

//Function To Calculate Time To Save  

function TTS(currentage, retirementage) {

    currentage = parseFloat(currentage);
    retirementage = parseFloat(retirementage);
    return (retirementage - currentage);
}
//end

Javascript Function To calculate Real Rate Of Return

//Function To calculate Real Rate Of Return
function RROR(futureinflationrate, futurerfrexpected) {
    var result;
    futureinflationrate = eval(futureinflationrate / 100);
    futurerfrexpected = eval(futurerfrexpected / 100);
    result = eval((100 * (1 + futurerfrexpected) - 100 * (1 + futureinflationrate)) / (100 * (1 + (futureinflationrate * 100) / 100)) * 100);
    return result;
}
//end Of RROR

How to get number upto n decimal places in javascript use .toFixed function

kittycorpusrequiredatthetimeofretirement.toFixed(n)

e.g. n=0,n=2