dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: Converter that needs a decimal place limiter

  1. #1
    Join Date
    Jul 2011
    Posts
    3

    Cool Converter that needs a decimal place limiter

    I am building a simple converter, converting between mm's and inches and I need to round off the results to 2 and 3 decimal places respectively.
    The code can be seen below can anyone show me how to get my results with decimal places:

    <head>
    <SCRIPT TYPE="text/javascript">
    <!--
    function popup(mylink, windowname)
    {
    if (! window.focus)return true;
    var href;
    if (typeof(mylink) == 'string')
    href=mylink;
    else
    href=mylink.href;
    window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
    return false;
    }
    //-->
    </SCRIPT>

    <script language="JavaScript">
    <!--
    function mmConverter(){
    document.converter.inch.value = document.converter.mm.value / 25.4
    }
    function inchConverter(){
    document.converter.mm.value = document.converter.inch.value * 25.4
    }
    //-->
    </script>
    </head>
    <body>
    <p>Just enter the amount in either box and press enter or click the convert button<br> to display the conversion in the other box</p>
    <br>
    <br>
    <form name="converter">
    <strong>mm(s):&nbsp&nbsp&nbsp</strong> <input type="text" name="mm" onChange="mmConverter() "><br /><br><br>
    <strong>Inch(es):</strong> <input type="text" name="inch" onChange="inchConverter() "><br /><br><br>
    <input type="button" value="Convert!" /><br><br><br><br><br><br>
    </form>
    </body>

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    You can use Math.round
    HTML Code:
    var value=3.1415925,decim=1000; // For 3 decimal
    var val=Math.round(value*decim)/decim;
    alert (value+'  '+val );

  3. #3
    Join Date
    Jul 2011
    Posts
    3
    Thank you Julien, I am having problems with this (forgive me I'm a bit of an amateur) I take it I implement this after

    function mmConverter(){
    document.converter.inch.value = document.converter.mm.value / 25.4

    but when I do I get another pop up with 3.1415925.. and 3.142 come up.
    How do I implement this so the result ends up in the respective box?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    You change the first function like this (for 2 decimal).

    HTML Code:
    function mmConverter(){var d=100,v=document.converter.mm.value/25.4;
          document.converter.inch.value = Math.round(v*d)/d;
    }
    You will probably able to change the second function. You can too remove the input button.

  5. #5
    Join Date
    Jul 2011
    Posts
    3

    Thumbs up Thank You

    Thank you, that did the trick.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles