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

标签 javascript

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

相关文章:

javascript - 键码返回未定义并且返回 0

javascript - 将焦点设置到文本区域后如何防止冒泡事件?

javascript - 删除附加的 html 代码

javascript - RxJS switchMap 与 take 运算符

javascript - 如何在向下滚动时缩小动画标题并在向上滚动时放大它?

javascript - Angular 应用程序中的 Angular 元素在优化时会产生无限重新加载问题

Javascript 谷歌地图,按经度和纬度坐标显示图钉

javascript - 动态加载不在类型为 ="module"的脚本标记中的模块?

javascript - 在 php 中更新后,Sweetalert JS 重定向窗口位置

javascript - 在 Node 中使用串联的简单 JS require/include/import 工具