Web Analytics Made Easy -
StatCounter rounding numbers - CodingForum

Announcement

Collapse
No announcement yet.

rounding numbers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • rounding numbers

    i'm currently using Math.round() to round off some percentages, however it will only round to the nearest integer. seeing as i'm working with currency i could do with being able to round to 2 decimal places.

    i've done a bit of research on the round() method but can't see any arguments to add for number of decimal places etc but i would assume there is some method of doing this as its quite a major function in most applications dealing with numbers.

    anybody know how to do this?

    thanks, dd/mm/yy

  • #2
    Hi there dd/mm/yy,

    Use toFixed(2)
    Here is an example...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta content="mshtml 6.00.2800.1264" name="generator" />
    <title>calculations</title>
    <style type="text/css">
    <!--
    body
       {
        background-color:#aaaaff;
        font-family:arial;
        font-size:16px;
       }
    div#calcs
       {
        width:200px;
        padding:40px 10px 40px 40px ;
        background-color:#ffffff;
        border:solid 1px #000000;
        position:absolute;
        left:25%;
        top:20%;
       }
    input#one, #two,#three
       {
        width:120px;
        font-size:16px;
        text-align:center;
       }
    input#button, #reset
       {
        width:56px;
       }
    //-->
    </style>
    <script type="text/javascript">
    <!--
    function showIt()
    {
    var stuffone=document.getElementById("one").value;
    var stufftwo=document.getElementById("two").value;
    var result=(stuffone*stufftwo/100).[color=red]toFixed(2)[/color];
    document.getElementById("three").value=result;
    }
    //-->
    </script>
    </head>
    <body onload="document.getElementById('two').focus()">
    <div id="calcs">
    <form action="">
    <div>
    <input type="text"id="one"/>&#160;&#160;Pounds<br /><br />
    <input type="text"id="two"/>&#160;&#160;Percent<br /><br />
    <input type="text"id="three"/>&#160;&#160;Result<br /><br />
    <input type="button"id="button"value="send"onclick="showIt();"/>&#160;
    <input type="reset"id="reset"/>
    </div>
    </form>
    </div>
    </body>
    </html>
    cthead
    ~ the original bald headed old fart ~

    Comment


    • #3
      Have a look at the FAQ thread. It answers this question.


      Don't use .toFixed. It is broken in ie5m, ie5.0w, ie5.5w. Some numbers will give results that are really, really off.
      liorean <[[email protected]]>
      Articles: RegEx evolt wsabstract , Named Arguments
      Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
      Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

      Comment

      Working...
      X