我使用在线教程编写以下快速贷款计算器,因为我有非常基本的编码技能。它工作得很好,我唯一需要改变的是所有三个输出量都需要数字中的空格...例如,它应该输出“400 000”而不是“400000”。如何添加空格或昏迷以使大数字更具可读性?
<form oninput="loanval.value=loan.value; periodval.value=period.value; paymentval.value=Math.floor(loan.value / period.value)">
<label>Loan Amount</label>
<input type="range" id="loan" name="loan" min="5000" max="400000" step="5000">
<output name="loanval" for="loan">0</output>
<label>Loan Period</label>
<input type="range" id="period" name="period" min="1" max="15">
<output name="periodval" for="period">0</output>
<label>Your monthly repayment will be:
<output name="paymentval">0</output>
</form>
答案 0 :(得分:1)
更改
paymentval.value=Math.floor(loan.value / period.value)
到
paymentval.value=Math.floor(loan.value / period.value).toLocaleString()
如果您需要特定国家/地区格式化,则可以添加区域设置。 E.G。
paymentval.value=Math.floor(loan.value / period.value).toLocaleString('de-DE')
答案 1 :(得分:0)
答案 2 :(得分:0)
我会将输出标记更改为范围并执行此操作:
var num = $('#loan').val();
num = numberWithCommas(num);
$('#loanval').text(num);
//from http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
你的HTML将是:
<label>Loan Amount</label>
<input type="range" id="loan" name="loan" min="5000" max="400000" step="5000">
<span id ="loanval">0</span>
您也可以为所有输出执行此操作。然后添加它们你会做类似的事情:
//for each span...
var numberToAdd = $('#loanval').val();
numberToAdd = parseInt(numberToAdd);
$('#paymentval').text(numberToAdd);
<span id="paymentval">0</span>