Help needed for the weight conversion script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JS

    #1

    Help needed for the weight conversion script

    I've managed to create a simple script to convert between metric and
    imperial. It works for CMS to INCHES and vice versa but not for KGS to
    STONES/POUNDS. Can anyone shed any light on this?

    Here's my code (CMS and INCHES which is working):

    <input name="CM" type="text" VALUE="" size="3" maxlength="3"
    onChange="eval( 'IN.value = ' + this.form.IN_ex pr.value)">
    cms <em>or</em>
    <input name="IN" type="text" VALUE="" size="3" maxlength="3"
    onChange="eval( 'CM.value = ' + this.form.CM_ex pr.value)">
    inches<INPUT TYPE="hidden" NAME="CM_expr" VALUE="(Math.ro und(2.54 *
    IN.value))">
    <INPUT TYPE="hidden" NAME="IN_expr" VALUE="(Math.ro und(CM.value /
    2.54))">

    Here's my code for KGS to STONES/POUNDS (not working):

    <input name="KGM" type="text" VALUE="" size="3" maxlength="3"
    onChange="eval( 'ST.value = ' + this.form.ST_ex pr.value);
    eval('PNDS.valu e = ' + this.form.PNDS_ expr.value)">
    KGM <em>or</em>
    <input name="ST" type="text" VALUE="" size="3" maxlength="3"
    onChange="eval( 'KGM.value = ' + this.form.KGM_e xpr.value)">St
    <input name="PNDS" type="text" VALUE="" size="3" maxlength="3"
    onChange="eval( 'KGM.value = ' + this.form.KGM_e xpr.value)">
    lbs
    <INPUT TYPE="hidden" NAME="KGM_expr" VALUE="(Math.ro und((ST.value * 14)
    + PNDS.value) * 0.4536)">
    <INPUT TYPE="hidden" NAME="ST_expr" VALUE="(Math.ro und(KGM.value /
    0.4536) / 14)"><INPUT TYPE="hidden" NAME="PNDS_expr "
    VALUE="(Math.ro und(KGM.value / 0.4536) - ST.value)">

    Firstly, I only want an integer in the result but am getting a decimal
    to about 6 places.

    Secondly, it appears that the + in (Math.round((ST .value * 14) +
    PNDS.value) * 0.4536) is appending the PNDS.value to the ST.value * 14
    so if PNDS.value is 0 and ST.value is 12 it calculates using 120.

    I've spent a good few hours trying to get it to work or find the
    suitable code but just can't find it.

    Many thanks in anticipation.

  • McKirahan

    #2
    Re: Help needed for the weight conversion script

    "JS" <google@stanton .uk.net> wrote in message
    news:1111594625 .495977.106700@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > I've managed to create a simple script to convert between metric and
    > imperial. It works for CMS to INCHES and vice versa but not for KGS to
    > STONES/POUNDS. Can anyone shed any light on this?[/color]

    [snip]

    This link may already do what you want; look at the code behind.

    Online converter for units of mass and weight. Pounds, kilograms, carats, and more. Instantly convert any unit to all others.


    Here are some other calculator links:






    Comment

    • JS

      #3
      Re: Help needed for the weight conversion script

      Thanks for the links. I think I've already found those sites on my
      travels and they seemed a lot more complicated than I actually need. I
      could really do with someone who's a lot better at Javascript to look
      at my syntax and tell me where it's wrong.

      Thanks again, much appreciated.

      Comment

      • McKirahan

        #4
        Re: Help needed for the weight conversion script

        "JS" <google@stanton .uk.net> wrote in message
        news:1111600242 .941202.65720@l 41g2000cwc.goog legroups.com...[color=blue]
        > Thanks for the links. I think I've already found those sites on my
        > travels and they seemed a lot more complicated than I actually need. I
        > could really do with someone who's a lot better at Javascript to look
        > at my syntax and tell me where it's wrong.
        >
        > Thanks again, much appreciated.
        >[/color]

        How about:

        <html>
        <head>
        <title>titles.h tm</title>
        <script type="text/javascript">
        function titles() {
        var what = "";
        for (var i=0; i<44; i++) {
        what += " " + unescape("%A0") ;
        }
        document.title += what;
        }
        </script>
        </head>
        <body onload="titles( )">
        </body>
        </html>


        Comment

        • RobG

          #5
          Re: Help needed for the weight conversion script

          JS wrote:[color=blue]
          > I've managed to create a simple script to convert between metric and
          > imperial. It works for CMS to INCHES and vice versa but not for KGS to
          > STONES/POUNDS. Can anyone shed any light on this?
          >
          > Here's my code (CMS and INCHES which is working):
          >
          > <input name="CM" type="text" VALUE="" size="3" maxlength="3"
          > onChange="eval( 'IN.value = ' + this.form.IN_ex pr.value)">
          > cms <em>or</em>[/color]

          The correct abbreviation for centimetre is "cm" and the correct
          abbreviation for kilograms is "kg".
          [color=blue]
          > <input name="IN" type="text" VALUE="" size="3" maxlength="3"
          > onChange="eval( 'CM.value = ' + this.form.CM_ex pr.value)">
          > inches<INPUT TYPE="hidden" NAME="CM_expr" VALUE="(Math.ro und(2.54 *
          > IN.value))">
          > <INPUT TYPE="hidden" NAME="IN_expr" VALUE="(Math.ro und(CM.value /
          > 2.54))">[/color]

          Almost any use of 'eval' is wrong or not required, see below.
          [color=blue]
          >
          > Here's my code for KGS to STONES/POUNDS (not working):
          >[/color]
          [...][color=blue]
          > <INPUT TYPE="hidden" NAME="ST_expr" VALUE="(Math.ro und(KGM.value /
          > 0.4536) / 14)"><INPUT TYPE="hidden" NAME="PNDS_expr "
          > VALUE="(Math.ro und(KGM.value / 0.4536) - ST.value)">
          >
          > Firstly, I only want an integer in the result but am getting a decimal
          > to about 6 places.[/color]

          That is because in the expression:

          (Math.round(KGM .value / 0.4536) / 14)

          Math.round applies to KGM.value/0.4536. You then divide by 14 to
          create your decimal. Fixing syntax and guessing what you
          intended:

          Math.round((thi s.form.KGM.valu e/0.4536)/14)
          [color=blue]
          >
          > Secondly, it appears that the + in (Math.round((ST .value * 14) +
          > PNDS.value) * 0.4536) is appending the PNDS.value to the ST.value * 14
          > so if PNDS.value is 0 and ST.value is 12 it calculates using 120.[/color]

          Values from inputs are always treated as strings initially so
          you must force them to become numbers if that is what is
          required.

          Your use of "ST.value * 14" forces ST.value to be a number, but
          PNDS.value is a string, so it's concatenated, not added. To
          force it to be a number add a '+' as follows:

          ...((this.form. ST.value*14 + +this.form.PNDS .value)*0.4536)

          You can do other tricks too (multiply by 1, subtract instead of
          add, etc.), but this way makes it obvious what you are doing.

          Below is a different version that does away with 'eval' and the
          hidden inputs, but beware that you don't do any validation of
          input. There are also issues with the precision of JavaScript
          mathematics, you should read here to get more information about
          doing maths with JavaScript:

          <URL:http://www.merlyn.demo n.co.uk/js-maths.htm>


          <form action="">
          <table>
          <tr>
          <td colspan="2">
          All values rounded to nearest whole number
          </td>
          </tr><tr>
          <td>
          <input name="CM" type="text" VALUE="0" size="3"
          maxlength="3" onChange="
          // 1 inch = 2.54 cm
          this.form.IN.va lue = Math.round(this .value/2.54);
          "> cm <em>converts to</em>

          </td><td>

          <input name="IN" type="text" VALUE="0" size="3"
          maxlength="3" onChange="
          // 1 inch = 2.54 cm
          this.form.CM.va lue = Math.round(this .value*2.54);
          "> inches

          </td>
          </tr><tr>
          <td>

          <input name="KG" type="text" VALUE="0" size="3"
          maxlength="3" onChange="
          // 1 kg = 2.20462262 lbs
          var x = Math.round(this .value*2.204622 6);
          // 14 lbs = 1 stone
          this.form.ST.va lue = Math.floor(x/14);
          this.form.LBS.v alue = x%14;
          "> kg <em>converts to</em>

          </td><td>

          <input name="ST" type="text" VALUE="0" size="3"
          maxlength="3" onChange="
          this.form.KG.va lue = Math.round((thi s.value*14
          + +this.form.LBS. value)/2.2046226);
          ">St
          <input name="LBS" type="text" VALUE="0" size="3"
          maxlength="3" onChange="
          this.form.KG.va lue = Math.round((thi s.form.ST.value *14
          + +this.value)/2.2046226);
          ">lbs</p>

          </td>
          </tr><tr>
          <td colspan="2">
          <input type="reset" onclick="if(thi s.blur) this.blur();">
          </td>
          </tr>
          </table>
          </form>


          --
          Rob

          Comment

          • JS

            #6
            Re: Help needed for the weight conversion script

            Hi Rob

            Fantastic reply. I shall go through what you've suggested later today
            and let you know how it goes.

            Many thanks to everyone who's responded to this thread.

            Jon

            Comment

            • JS

              #7
              Re: Help needed for the weight conversion script

              Hi again Rob

              Used your code and it worked perfectly so many, many thanks. It really
              is much appreciated.

              All the best

              Jon

              Comment

              • Dr John Stockton

                #8
                Re: Help needed for the weight conversion script

                JRS: In article <1111594625.495 977.106700@o13g 2000cwo.googleg roups.com>
                , dated Wed, 23 Mar 2005 08:17:05, seen in news:comp.lang. javascript,
                JS <google@stanton .uk.net> posted :[color=blue]
                >
                >Secondly, it appears that the + in (Math.round((ST .value * 14) +
                >PNDS.value) * 0.4536) is appending the PNDS.value to the ST.value * 14
                >so if PNDS.value is 0 and ST.value is 12 it calculates using 120.[/color]


                FAQ, 4.21, refers; read the newsgroup FAQ before posting.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • JS

                  #9
                  Re: Help needed for the weight conversion script

                  My apologies for posting this but I spent many hours searching for the
                  solution but was unable to. If it's in the FAQ then I was unable to
                  locate it. But, having posted here, RobG gave me exactly what was
                  needed so I feel that this group has served the exact purpose it
                  should. I'm not one for posting without first trying to solve the
                  problem.

                  Comment

                  Working...