Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Rounding query

  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rounding query

    I am very new to JavaScript and have been looking how to round a number to two decimal places.

    From looking at the tutorial at http://www.javascriptkit.com/javatutors/round.shtml
    I added "var result=Math.round(tempnum*100)/100000}"
    to the final line of my page, but it does not work!!


    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function calc(f)
    {
    vx = parseInt(f.x.value);
    da = new Date(f.a.value);
    db = new Date(f.b.value);
    dd = (db.valueOf()-da.valueOf())/1000/60/60/24;
    vc = parseInt(f.c.value);
    vd = parseInt(f.d.value);

    f.answer.value=(vx/365)*dd-vc+vd
    function roundoff(amount) {
    return Math.round(amount)
    function decimal(num){
    var tempnum=num
    for (i=0;i<tempnum.length;i++){
    if (tempnum.charAt(i)=="."){
    break
    }
    if (tempnum.charAt(tempnum.length-2)=="."){
    num=tempnum+"0"
    }
    else{
    num=tempnum
    }
    var result=Math.round(num*100)/100};

    }
    }
    }
    }
    //-->
    </SCRIPT>


    Many thanks,
    Eski

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is an example that works for me:
    <html>
    <head>
    <title>Rounding Numbers</title>
    <SCRIPT LANGUAGE="JavaScript">
    function calc(formid)
    {
    re=/\./gi
    // convert the two values to numbers
    numa=parseFloat(formid.txta.value)
    numb=parseFloat(formid.txtb.value)
    numc=numa+numb
    formid.txtc.value=numc
    // round to two decimal positions
    var number=Math.round(numc * 100)/100
    // convert back to string
    var numd=number.toString()
    nume=numd.split(".")
    numelen=nume.length
    if(numelen<2) // if whole number add decimal point and two zeros
    {
    numd+=".00"
    }
    else
    {
    if(nume[1].length<2) // if only one digit after decmal point add zero
    {numd+="0"}
    }
    formid.txtd.value=numd
    }
    </SCRIPT>
    </head>
    <body>
    <FORM NAME="myform">
    First number <INPUT TYPE="text" NAME="txta" SIZE="10">
    <br>
    Second Number <INPUT TYPE="text" NAME="txtb" SIZE="10">
    <br>
    Before rounding<INPUT TYPE="text" NAME="txtc" SIZE="15">
    <br>
    After rounding <INPUT TYPE="text" NAME="txtd" SIZE="15">
    <br>
    <INPUT TYPE="button" NAME="but1" VALUE="Add" onClick="calc(this.form)">
    </FORM>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, and thanks for you help, but....

    I see how your line:
    var number=Math.round(numc * 100)/100

    works for you, but when I use this on my page (substiuting numc), it still doesn't work!!!

    I have used Math.round function, but followed by (amount) . is this what is causing the problem?

    Sorry to sound such a dunce!!!

    Eski

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Don't know what all that code is about.... here is the basics.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <script language="JavaScript" type="text/javascript">
    <!--
    roundedNum = 25.12543;
    alert ("Number starts out as " + roundedNum);
    
    roundedNum = Math.round(roundedNum*100)/100;
    alert ("Rounded to 2 places. = " + roundedNum);
    //-->
    </script>
    
    </body>
    </html>
    The technique: "grab" two decimal digits by multiplying by 100. Round the result. Undo the "grab" by dividing by the same value - 100 in this case. Ta da!


    Also, you've got functions defined inside of other functions (look where the terminating brackets are!). That's a no-no. Fix that and maybe your problem is fixed.


  •  

    Posting Permissions

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