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
    Jun 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I Round up numbers in this conversion table

    I am using this code to convert liquid volume but it converts too many decimal places. Could someone show me how to reduce the conversion to 1 or 2 decimal places?

    Many thanks

    // JavaScript Document
    <!--

    function nofocus()
    {
    document.convert.InUnit.focus()
    }
    var mlValue = 1
    var ltrValue = 1000
    var fozValue = 28.4
    var cupValue = 236
    var ptValue = 568.261

    function toCM()
    {
    var i = document.convert.unit.selectedIndex
    var thisUnit = document.convert.unit.options[i].value
    if (thisUnit == "ML")
    {
    document.convert.ml.value = document.convert.InUnit.value
    }

    else if(thisUnit == "LTR")
    {
    document.convert.ml.value = document.convert.InUnit.value * ltrValue
    }

    else if(thisUnit == "FOZ" )
    {
    document.convert.ml.value = document.convert.InUnit.value * fozValue
    }

    else if(thisUnit == "CUP" )
    {
    document.convert.ml.value = document.convert.InUnit.value * cupValue
    }

    else if(thisUnit == "PT" )

    {
    document.convert.ml.value = document.convert.InUnit.value * ptValue
    }


    toAll()
    }
    function toAll()
    {
    var m = document.convert.ml.value
    document.convert.ltr.value = m / ltrValue
    document.convert.foz.value = m / fozValue
    document.convert.pt.value = m / ptValue
    document.convert.cup.value = m / cupValue
    }

    //-->

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts
    Try...
    Code:
    document.convert.ltr.value = (m / ltrValue).toFixed(2);

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Try...
    Code:
    document.convert.ltr.value = (m / ltrValue).toFixed(2);
    The above worked perfectly, many thanks for your time and help.

    Cheers!

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    0
    Thanked 6 Times in 6 Posts
    you can also use
    DecimalFormat df = new DecimalFormat("0.00");

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by shyagrawal View Post
    you can also use
    DecimalFormat df = new DecimalFormat("0.00");
    No. that is Java, not Javascript.

    As well as toFixed() which results in a string value you can use

    Code:
    <script type = "text/javascript">
    
    roundNumber = function(num, dec) {
    return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    }
    
    alert (roundNumber(123.45678,2));
    
    </script>
    Last edited by Philip M; 10-08-2012 at 04:41 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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