SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Bedford, UK
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to round figures to 2 decimal places

    Hi All,

    I have obtained a Javascript calculator from a website, but the total calculation field shows up like 15 decimal places. Can someone show me how to change itso it shows only 2 decimal places.

    <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;
    document.calc.pay.value = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
    }

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

    }

    // -->
    </script>

    <span class="style13">The results of this loan payment calculator are for comparison purposes only.
    They will be a close approximation of actual loan
    repayments if available at the terms entered, from a financial institution. This
    is being
    provided for you to plan your next loan application. To use, enter values
    for the
    Loan Amount, Number of Months for Loan, and the Interest Rate (e.g.
    7.25), and
    click the Calculate button. Clicking the Reset button will clear entered
    values.
    </span>
    <p>
    <center>
    <form name=calc method=POST>
    <table width=60% border=0>
    <tr><th bgcolor="#aaaaaa" width=50%><span class="style12">Description</span></th>
    <th bgcolor="#aaaaaa" width=50%><span class="style12">Data Entry</span></th>
    </tr>
    <tr bgcolor="#CCCCCC"><td>Loan Amount</td>
    <td align=right><input
    type=text name=loan
    size=10></td></tr>
    <tr bgcolor="#CCCCCC"><td>Loan Length in Months</td>
    <td
    align=right><input type=text
    name=months size=10></td></tr>
    <tr bgcolor="#CCCCCC"><td>Interest Rate</td>
    <td align=right><input
    type=text name=rate
    size=10></td></tr>
    <tr><td bgcolor="#CCCCCC">Monthly Payment</td>
    <td bgcolor="#FFFF00"
    align=right><em>Calculated</em> <input
    type=text name=pay size=10></td></tr>
    <tr bgcolor="#FFFFFF"><td height="42"align=center><input type=button onClick='showpay()'
    value=Calculate></td><td align=center><input type=reset
    value=Reset></td></tr>
    </table>
    </form>
    <font size=1>Enter only numeric values (no commas), using decimal points
    where needed.<br>
    Non-numeric values will cause errors.</font>
    </center></td>


    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Brunei
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript has a toFixed () function in the Number class that'll do what you want.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Bedford, UK
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I'm very new to Javascript. Any chance you could show me how?

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Brunei
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      numToRound = 25.5555555
      num = new Number (numToRound);
      numRounded = num.toFixed (2);

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Bedford, UK
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, now where do I put it?

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Bedford, UK
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bump - just need to know where to put the code. please

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pocketsize
    not try to steal your thunder but Oracle seems ancious


    PHP Code:
    document.calc.pay.value = (princ intr / (- (Math.pow(1/(intr), term)))).toFixed(2); 

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Bedford, UK
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many Thanks. I owe you, both!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •