新手刚学微信小程序,想做个这个题目:
设计一个小程序输入本金、利率。默认本金10000,利率3%,5年后得到多少钱?
但做完发现利息计算正确但结果却总是初始本金与利息拼接的一个数字就这样:
这是代码,麻烦大佬看看问题出在哪里:
index.js
Page({ /**
* 页面的初始数据
*/ data: {
flag: true,
name: '',
principal: '',
interest_rate: '',
access_period: '',
interest:'',
Total_principal_after_deposit_and_withdrawal_period:''
},
nameInput: function (e) {
this.setData({
name: e.detail.value
});
},
principalInput: function (e) {
this.setData({
principal: e.detail.value
});
},
interest_rateInput: function (e) {
this.setData({
interest_rate: e.detail.value
});
},
access_periodInput: function (e) {
this.setData({
access_period: e.detail.value
});
},
mysubmit: function () {
var benjin = this.data.principal;
var lilu = this.data.interest_rate;
var lixi = 0;
for(var i = 1;i <= this.data.access_period; i++ ){
lixi += benjin*lilu;
benjin = benjin + lixi;
}
this.setData({
Total_principal_after_deposit_and_withdrawal_period: benjin,
interest: lixi,
flag: false
});
}
}
)
index.wxml
<view class='box'>
<view class='title'>利息计算器</view>
<input placeholder="请输入你的名字" placeholder-class="placeholder" bindinput='nameInput'></input>
<input placeholder="请输入本金" placeholder-class="placeholder" bindinput='principalInput' type='number'></input>
<input placeholder="请输入利率" placeholder-class="placeholder" bindinput='interest_rateInput' type='number'></input>
<input placeholder="请输入存取年期" placeholder-class="placeholder" bindinput='access_periodInput' type='number'></input>
<button bindtap='mysubmit'>提交</button>
<view hidden='{{flag}}' class='content'>
<view class='content-item'>姓名:{{name}}</view>
<view class='content-item'>每月本金:{{principal}}</view>
<view class='content-item'>利率:{{interest_rate}}</view>
<view class='content-item'>所得利息总计:{{interest}}</view>
<view class='content-item'>存取年限后总本金:{{Total_principal_after_deposit_and_withdrawal_period}}</view>
</view>
</view>
var benjin = this.data.principal; var lilu = this.data.interest_rate; 这两个转换成数字试试
又有新问题了,当利率设为小数时,利息输出结果为null,懵了