www.webdeveloper.com
Results 1 to 6 of 6

Thread: Help with Rounding Up - My first post

Hybrid View

  1. #1
    Join Date
    May 2013
    Posts
    3

    Help with Rounding Up - My first post

    Hi all My first post.

    I am developing a simple web page form where the visitor can input height and width and the script calculates the area and price.

    The script below does this fine.

    However, I would like to roundup the answer to the next integer for example if price = 45.678 final output would be 46.

    I have tried Math.round(x) but the syntax totally defeats me.

    I would be grateful if someone would give me a hand and post the required change to my script below.

    Hopefully not a big ask.

    Regards

    -------------------------

    <script type="text/javascript" language="Javascript">

    var h1 = 0;
    var w2 = 0;
    var price = 0;

    function calculate () {
    // The Calculation: change operator in this equation to change the calculation
    price = h1 * w2 * .0005;

    var elem;
    elem = document.getElementById("h1");
    elem.value = h1;
    elem = document.getElementById("w2");
    elem.value = w2;

    elem = document.getElementById("price");
    elem.value = price;
    }

    function setValue(elem) {
    var val = 0.0;
    if (elem.value != "") {
    val = parseFloat(elem.value);
    }
    else {
    val = 0.0;
    }
    switch (elem.id) {
    case "h1": h1 = val; break;
    case "w2": w2 = val; break;
    }
    calculate();
    }
    </script>


    --------------

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    Code:
    elem = document.getElementById("price");
    elem.value = Math.round(price);
    Last edited by Padonak; 05-17-2013 at 02:33 PM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    May 2013
    Posts
    3
    Padonak

    Many thanks for such a quick reply.

    Where exactly does your addition fit in my script.

    Ta

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    there is only one place in your script where you set document.getElementById("price") value, am i wrong?
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    May 2013
    Posts
    3
    Panonak

    Many thanks for your help - All appears to be working as required - and yes there is only one place where "price" is set - Regards

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    good to hear it helped ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

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

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