我写了一个简单的JS函数,根据一个数字(称为平均月收入)计算社会保障福利。我正在努力解决在哪里以及如何强制数字不超过 2 位数字的问题。我还没有弄清楚表单验证,但那是另一次了。我的问题:在哪里以及如何使输出(我的 y 变量)的数字不超过 2 位数字?预先感谢您。
<form>
<input type="text" id="ami" placeholder="enter your AMI"><br><br>
<input type="button" name="calculateButton" onclick="ssCalculator()" value="Calculate">
</form>
<p id="results">
</p>
<script>
function ssCalculator() {
var pia90 = 926; //updated for 2019
var pia32 = 5583; //updated for 2019
var pia15 = 5583.01; //updated for 2019
var x = document.getElementById("ami").value;
if (x<pia90) {
let y = (.9*x);
document.getElementById("results").innerHTML = ("If you start receiving Social Security at <strong>age 62</strong> your monthly benefit would be $" + (y*.7) +".<br>");
document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 67</strong> your monthly benefit would be $" + (y) +".<br>");
document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 70</strong> your monthly benefit would be $" + (y*1.24) +".");
}
if (pia90<x && x<pia32) {
let y = (.9*pia90)+.32*(x-pia90);
document.getElementById("results").innerHTML = ("If you start receiving Social Security at <strong>age 62</strong> your monthly benefit would be $" + (y*.7) +".<br>");
document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 67</strong> your monthly benefit would be $" + (y) +".<br>");
document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 70</strong> your monthly benefit would be $" + (y*1.24) +".");
}
if (x>pia15) {
let y = (.9*pia90)+(.32*pia32)+.15*(x-pia15);
document.getElementById("results").innerHTML = ("If you start receiving Social Security at <strong>age 62</strong> your monthly benefit would be $" + (y*.7) +".<br>");
document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 67</strong> your monthly benefit would be $" + (y) +".<br>");
document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 70</strong> your monthly benefit would be $" + (y*1.24) +".");
}
}
</script>
最佳答案
关于javascript - 将数字强制为 2 位以进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54993414/