简体   繁体   中英

rounding variable and making it a currency

I am trying to get my outputs to round to two decimals places and have them formatted as a currency ie $23,923.23. I am unfortunately struggling to get it to work. I am very new to this and have tried googling and searching on here for an answer but to no avail. Any ideas?

<script language="JavaScript">
    <!--
    function showpay() {
        if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) ||
            (document.calc.months.value == null || document.calc.months.value.length == 0) ||
            (document.calc.rate.value == null || document.calc.rate.value.length == 0)) {
            document.calc.pay.value = "Incomplete data";
        } else {
            var princ = document.calc.loan.value;
            var term = document.calc.months.value;
            var intr = document.calc.rate.value / 1200;
            var intr2 = document.calc.rate2.value / 1200;
            document.calc.pay.value = princ * intr / (1 - (Math.pow(1 / (1 + intr), term)));
            document.calc.pay2.value = princ * intr2 / (1 - (Math.pow(1 / (1 + intr2), term)));

            var month1 = document.calc.pay.value;
            var month2 = document.calc.pay2.value;
            document.calc.difference.value = month1 - month2;

            var difference = document.calc.difference.value;
            document.calc.total.value = difference * term;

            var intr3 = document.calc.rate3.value / 1200 + 1;

            var intr4 = Math.pow(intr3, term)

            document.calc.portfolio.value = difference * ((intr4 - 1) / (intr3 - 1));

        }

        // payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))

    }

    // -->
</script>

<form name=calc method=POST>
    <table width=100% border=0>

        <tr>
            <th bgcolor="#aaaaaa" align=center>
                <font color=blue>Student Loan Refinance Calculator</font>
            </th>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Loan Amount <input type=number name=loan style="width: 100px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Loan Length in Months <input type=number name=months style="width: 75px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Old Interest Rate (7%=7) <input type=number name=rate style="width: 50px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>New Interest Rate <input type=number name=rate2 style="width: 50px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff" align=center><input type=button onClick='showpay()' value=Calculate></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Old Monthly Payment <input type=number name=pay style="width: 100px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>New Monthly Payment <input type=number name=pay2 style="width: 100px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Monthly Savings <input type=number name=difference style="width: 100px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Total Savings <input type=number name=total style="width: 100px"></td>
        </tr>

        <tr>
            <th bgcolor="#aaaaaa" width=100%>
                <font color=blue>If you invest the monthly savings</font>
            </th>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Annual Return (7%=7) <input type=number name=rate3 style="width: 50px"></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff" align=center><input type=button onClick='showpay()' value=Calculate></td>
        </tr>

        <tr>
            <td bgcolor="#ffffff">
                <font color=blue>Portfolio Value <input type=number name=portfolio style="width: 100px"></td>
        </tr>

    </table>
</form>

Try the usage of AngularJS. Here you have a currency-filter that formats automatically to a currency.

将其舍入到两位小数,使用toFixed()函数。

n.toFixed(2)

Intl.NumberFormat can do all the work and be customized for specific countries. This is just an example:

var value = 1234567.809;

var locale = "en-au";
var options = {
   style: "currency",
   currency: "AUD",
   minimumFractionDigits: 2,
   maximumFractionDigits: 2
}
var nf = new Intl.NumberFormat(locale, options).format;
console.log( nf(value) );   //= $1,234,567.81

The downside is that it could lack support in browsers such as Safari, although there is a polyfill listed on github which overcomes this. Of course if the page is on an intranet that has standardized on a supporting browser, there is no problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM