将数字强制为2位

时间:2019-03-04 23:55:38

标签: 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>

3 个答案:

答案 0 :(得分:3)

您可以使用toFixed()方法。

示例:

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

其他用法信息:

MDN

答案 1 :(得分:0)

尝试一下:

{{1}}

答案 2 :(得分:0)

只需使用toFixed()功能更多的information

<form>
    <input type="number" 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).toFixed(2) +".<br>");
            document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 67</strong> your monthly benefit would be $" + (y).toFixed(2) +".<br>");
            document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 70</strong> your monthly benefit would be $" + (y*1.24).toFixed(2) +".");
        }
        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).toFixed(2) +".<br>");
            document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 67</strong> your monthly benefit would be $" + (y).toFixed(2) +".<br>");
            document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 70</strong> your monthly benefit would be $" + (y*1.24).toFixed(2) +".");
        }
        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).toFixed(2) +".<br>");
            document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 67</strong> your monthly benefit would be $" + (y).toFixed(2) +".<br>");
            document.getElementById("results").innerHTML += ("If you start receiving Social Security at <strong>age 70</strong> your monthly benefit would be $" + (y*1.24).toFixed(2) +".");
        }
    }
</script>

对于某些基本的表单验证,只需将type中的input更改为number,它将仅接受数字