gpt4 book ai didi

javascript - 强制数字为 2 位数字以进行计算

转载 作者:行者123 更新时间:2023-11-29 18:44:19 26 4
gpt4 key购买 nike

我写了一个简单的 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>

最佳答案

您可以使用 toFixed() 方法。

例子:

let y = (.9*x).toFixed(2)

其他使用信息:

MDN

关于javascript - 强制数字为 2 位数字以进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54993414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com