首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >家庭通胀收支计算器

家庭通胀收支计算器

原创
作者头像
用户12885553
发布于 2026-11-11 11:50:30
发布于 2026-11-11 11:50:30
1110
举报
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>家庭收支通胀模拟器</title>
<style>
*{box-sizing:border-box;margin:0;padding:0;font-family:system-ui,sans-serif}
body{padding:16px;background:#f6f7f9;color:#222}
.card{background:#fff;border-radius:12px;padding:16px;margin-bottom:14px;box-shadow:0 2px 8px #00000012}
h2{font-size:18px;margin-bottom:12px;color:#334}
.row{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-bottom:10px}
label{display:block;font-size:14px;margin-bottom:4px;color:#555}
input{width:100%;padding:8px;border:1px solid #ddd;border-radius:8px;font-size:16px}
.btngroup{display:flex;gap:8px;margin:12px 0}
button{flex:1;padding:10px;border:none;border-radius:8px;font-size:15px;cursor:pointer}
.btn-low{background:#cce5ff;color:#004085}
.btn-base{background:#e2e3e5;color:#383d41}
.btn-high{background:#f8d7da;color:#721c24}
.btn-calc{background:#2563eb;color:#fff;margin-top:8px}
#result{white-space:pre-line;padding:12px;background:#f0f4fa;border-radius:8px;font-size:15px;line-height:1.6}
</style>
</head>
<body>
<div class="card">
<h2>基础收支设置</h2>
<div class="row">
<div>
<label>每月固定收入(元)</label>
<input id="income" type="number" value="12000">
</div>
<div>
<label>每月固定支出(元)</label>
<input id="expense" type="number" value="7000">
</div>
</div>
<div class="row">
<div>
<label>当前现金储备(元)</label>
<input id="cash" type="number" value="500000">
</div>
<div>
<label>年化理财收益率(%)</label>
<input id="rate" type="number" step="0.01" value="3.0">
</div>
</div>
<div class="row">
<div>
<label>模拟年限(年)</label>
<input id="year" type="number" value="30">
</div>
<div>
<label>通胀情景(一键切换)</label>
<div class="btngroup">
<button class="btn-low" onclick="setInflation(1.5)">低1.5%</button>
<button class="btn-base" onclick="setInflation(2.5)">基准2.5%</button>
<button class="btn-high" onclick="setInflation(4.0)">高4.0%</button>
</div>
</div>
</div>
<div>
<label>年度通胀率(%)</label>
<input id="inflation" type="number" step="0.01" value="2.5">
</div>
<button class="btn-calc" onclick="calc()">开始模拟</button>
</div>
<div class="card">
<h2>模拟结果</h2>
<div id="result"></div>
</div>
<script>
let infl = 2.5;
function setInflation(v){
    infl = v;
    document.getElementById('inflation').value = v;
}
function calc(){
    const income = Number(document.getElementById('income').value);
    const expense = Number(document.getElementById('expense').value);
    let cash = Number(document.getElementById('cash').value);
    const rate = Number(document.getElementById('rate').value)/100;
    const year = Number(document.getElementById('year').value);
    const inf = Number(document.getElementById('inflation').value)/100;
    let out = `【情景:通胀 ${inf*100}%】\n`;
    let monthlyExp = expense;
    for(let y=1;y<=year;y++){
        // 当年12个月
        for(let m=0;m<12;m++){
            cash = cash*(1+rate/12) + income - monthlyExp;
        }
        // 每年支出随通胀上涨(收入这里默认不变,你后续可以改成收入同步通胀)
        monthlyExp = monthlyExp*(1+inf);
        if(y%5===0){
            out += `第${y}年末:现金 ${cash.toFixed(0)} 元,月支出 ${monthlyExp.toFixed(0)} 元\n`;
        }
    }
    out += `\n✅ ${year}年后最终现金:${cash.toFixed(0)} 元\n`;
    out += `💡 说明:本模型默认收入不随通胀上涨;仅支出逐年跟随通胀上升;理财按月复利。`
    document.getElementById('result').innerText = out;
}
</script>
</body>
</html>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档