淘客熙熙

主题:【原创】一个计算退休金(401k)的脚本程序。欢迎测试 -- poorfat

共:💬3
分页树展主题 · 全看首页 上页
/ 1
下页 末页
  • 家园 【原创】一个计算退休金(401k)的脚本程序。欢迎测试

    我一直想知道每月应该存多少钱进401k将来才够用,于是就写了这么一个jscript程序。网上类似功能的401k计算器很多,但是都太复杂,我这个笨人一看就晕了。自己写了个简单的,但又怕遗漏了什么,所以贴上来共各位高手批评指正。谢先。

    使用说明:

    1。输入参数:

    numYearsTillRetire = 30; // 过多少年以后退休

    numYearsToLiveAfterRetire =20; // 退休后在打算活几年(想想都觉得心寒哪)

    moneyAddedTo401KYearly =7000; // 平均每年存多少钱到401k账号里

    appreciationRate_compound = 0.1; // 401k 账号的年增值复利

    inflationRate_compound = 0.03; // 退休后的生活费通胀复利

    currentAverageYealyLivingExpense = 30000; // 当前每年生活费开销

    2。运行方法

    把程序贴到记事本(notepad.exe)里,先保存好(如 my401k.js),再将此程序打开。

    上述参数都已经列在了程序的头部。更改并填入您自己认为合适的数值

    打开cmd.exe窗口,运行“cscript my401k.js”。

    显示结果与下列类似:

    Microsoft (R) Windows Script Host Version 5.6

    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    Total 401k savings till retirement =[1266604]

    first retire year living cost= [72818]

    Total Retirement living cost= [2088161]

    在这个例子里,退休金缺口是约八万多美元。还得继续努力挣钱啊。

    3。程序如下:

    var numYearsTillRetire = 30;

    var numYearsToLiveAfterRetire =20;

    var moneyAddedTo401KYearly =7000;

    var appreciationRate_compound = 0.1; // applied to 401k account

    var inflationRate_compound = 0.03; // applied to living expense increase after retirement

    var currentAverageYealyLivingExpense = 30000;

    main();

    function main()

    {

    var save =Total401kSavingsTillRetirement(moneyAddedTo401KYearly, numYearsTillRetire, appreciationRate_compound);

    WScript.Echo("Total 401k savings till retirement =["+ Math.round(save) +"]");

    var rc=TotalRetirementLivingExpenses(currentAverageYealyLivingExpense, numYearsTillRetire, numYearsToLiveAfterRetire, inflationRate_compound);

    WScript.Echo("Total Retirement living cost= ["+ Math.round(rc) +"]");

    }

    // end of main()

    function Total401kSavingsTillRetirement

    (

    annualAddition,

    yearsBeforeRetire,

    appreciationRate

    )

    /* this calculates the total 401k savings upto retirement

    the formula is:

    anual addition to 401k account * summation (1+appreciation rate) ^ years,

    where years = 1 to yearsBeforeRetire

    */

    {

    var p = PoweredSummation((1 + appreciationRate), yearsBeforeRetire);

    return annualAddition * (p);

    }

    // end of Total401kSavingsTillRetirement()

    function TotalRetirementLivingExpenses

    (

    currentYearlyExpense,

    yearsBeforeRetire,

    yearsAfterRetire,

    inflationRate

    )

    /* this calculates the total living expenses after retirement

    the formula is:

    first retire year expense * summation (1+inflationRate) ^ years,

    where years = 0 to yearsAfterRetire

    */

    {

    // compute the anticipated first retire year expense

    var firstRetireYearExpense = currentYearlyExpense;

    for (i = 1; i<=yearsBeforeRetire; i++)

    {

    firstRetireYearExpense = firstRetireYearExpense * (1+inflationRate);

    }

    WScript.Echo("first retire year living cost= ["+Math.round(firstRetireYearExpense) +"]");

    var ps =(1+PoweredSummation((1+inflationRate), yearsAfterRetire));

    return firstRetireYearExpense * ps;

    }

    // end of TotalRetirementLivingExpenses()

    function PoweredSummation(base, power)

    /*

    this computes the summation of base^i, where i is from

    1 to power, ie, base+base^2+base^3+...+base^power

    */

    {

    if (power<=0) {return 1;}

    var sum =0;

    for (i=1;i<=power;i++)

    {

    sum = sum + PowerbyInt(base, i);

    }

    return sum;

    }

    // end of PoweredSummation()

    function PowerbyInt(base, power)

    // returns the base to the power of "power"

    // power must be >=0

    {

    if (power <=0) {return 1;}

    var ret =1;

    for (j=1;j<=power; j++)

    {

    ret = ret * base;

    }

    return ret;

    }

    // end of PowerbyInt();

分页树展主题 · 全看首页 上页
/ 1
下页 末页


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河