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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problem Using toFixed

    My code converts a running pace to speed in mph. Works fine but posts too many digits after the decmal point in my form. Tried several things but I'm new to javascript and could use some help. Thanks! Oh, my browser is IE8, but the problem is more likely me than it...

    --Bill

    <code>

    <!--This does not work... form.mph.value.toFixed(4) = 60 / (pmin + psec / 60);
    Nor this... var mph; mph=mph.toFixed(4);
    -->

    <html>
    <head>
    <script language="javascript">
    function Speed(MIN, SEC, form)
    {
    var pmin = parseFloat(MIN);
    var psec = parseFloat(SEC);
    form.mph.value = 60 / (pmin + psec / 60);
    }

    function ClearForm(form)
    {
    form.input_pmin.value = "";
    form.input_psec.value = "";
    form.mph.value = "";
    }
    </script>
    </head>

    <body>

    <form name="pace-speed" method="post">

    <table style="font-size:14px; font-weight:bold;">

    <tr>
    <td>PACE</td>
    <td><input type="text" name="input_pmin" size="1">:</td>
    <td><input type="text" name="input_psec" size="1"></td>

    <td width="20"></td>

    <td><input TYPE="button" VALUE="Speed" onClick="Speed(this.form.input_pmin.value, this.form.input_psec.value, this.form)"></td>

    <td width="1"></td>

    <td><input type="text" name="mph" size="8"></td>

    <td width="2"></td>

    <td><input TYPE="reset" VALUE="Clear" onClick="clearForm(this.form)"></td>

    </tr>

    </table>
    </form>

    </body>
    </html>
    </code>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    did you try
    form.mph.value = (60 / (pmin + psec / 60)).toFixed(4); ?

  • Users who have thanked xelawho for this post:

    qwertyportne (07-31-2011)

  • #3
    New Coder
    Join Date
    Jun 2011
    Location
    Algeirs,Algeria
    Posts
    43
    Thanks
    5
    Thanked 3 Times in 3 Posts
    toFixed() method convert a number to string, so you can't use it before completing all your calculations, and -of course- toFixed() works only on numbers (primitive or objects).

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    did you try
    form.mph.value = (60 / (pmin + psec / 60)).toFixed(4); ?
    No, but that worked. Thanks!

    I remember trying... form.mph.value.toFixed(4) = etc and it did not work

    I just started trying this javascript stuff so I sure have a bunch to learn!

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    no problem - I know how you feel. I'm about 1% into it

    my take on why your solution didn't work is that the left side of the equals sign is for the new variable you are assigning a value to. the right side is used for modifications to pre-existing variables.

    I'm sure somebody has a much better explanation than this.

    the other bit was that - if you remember 8th grade math... BOMDAS - calculations within brackets get calculated first, which is why you needed to wrap the whole calculation in brackets, which calculates the number, then add the toFixed, which chops off the decimals.

    that's the way I figure it, anyway. hope that helps.


  •  

    Posting Permissions

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