编程语言
首页 > 编程语言> > 程序员之计算养老保险还是定投理财划算

程序员之计算养老保险还是定投理财划算

作者:互联网

作为程序员,想必大家都对于理财有一定的研究。一直想买养老产品,结果通过程序计算发现,其实养老保险并没有想象中的划算,以下通过计算得出的答案:

// 每2000每年保额获得的利息
const annuityRate = {
	26: 981.74 / 2000,
	27: 978.78 / 2000,
	28: 975.68 / 2000,
	29: 972.44 / 2000,
}

// 动态规划计算利息 - 模仿某养老金逐年定投
const fund = function (total, rate, aliveYear, nowYear, time) {
	// total总金  rate利息  aliveYear存货年龄  nowYear现年龄  time定投次数
	const year = total / time
	// 设置dp路径
	const dp = new Array(aliveYear - nowYear).fill(0)
	dp[0] = year * (1 + rate)
	for (let i = 1; i < time; i++) {
		dp[i] = (dp[i - 1] + year) * (1 + rate)
	}
	for (let i = time; i < dp.length; i++) {
		dp[i] = dp[i - 1] * (1 + rate)
	}
	return dp[dp.length - 1]
}

// 定投某养老金
const annuity = function (total, rate, aliveYear, nowYear) {
	const year = total / 20
	const value = (aliveYear - nowYear) * (year * annuityRate[nowYear])
	const interest = fund(value, rate, aliveYear, nowYear, aliveYear - nowYear)
	return interest + total
}

const total = 200000 // 定投总额
const rate = 0.04 // 理财产品利息
const aliveYear = 70 // 存活年龄
const nowYear = 27  // 现在年龄
const time = 20 // 定投年数


// 假设总过投20万,每年1万,今年27岁,70岁死亡,投保20年
const count1 = fund(total, rate, aliveYear, nowYear, time)
const count2 = annuity(total, rate, aliveYear, nowYear)

console.log('\x1B[36m%s\x1B[0m', `某养老金回本金额:${count2}`)
console.log('\x1B[31m%s\x1B[0m', `定投回本金额:${count1}`)

假设总过投20万,每年1万,今年27岁,70岁死亡,投保20年,理财利息是4%

结果得出:

当然这里只是简单的计算到了70岁所获得的金额,并没有把每年的花销算进去。其实两者的差距并不大,并且养老本金是需要身故后返回的,而理财并没有这个问题。。具体算法有什么不对或者没有考虑到的的情况,大家可以和我探讨下!

标签:划算,const,aliveYear,养老保险,程序员,rate,total,nowYear,dp
来源: https://blog.csdn.net/weixin_38189842/article/details/110475968