Problem rounding decimal in my code. HELP!

Discussion in 'JavaScript' started by pdorais75, Apr 29, 2008.

  1. #1
    I create a small script for calculate the amount of water a certain size of content can hold. But i can't figure what to add to my script for round the decimal to 2 max.

    Here à copie of the working script and html code, please show me what to modify to limit the amount of decimal.

    Thank you!


     
    pdorais75, Apr 29, 2008 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    Maybe this helps :)

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function round2(num) {
        var roundnum = 2;// up to 2 decimal numbers
        var rstr = '';
        var decstr = '';
        var numstr = num + '';
        var iod = numstr.indexOf('.');
        var i = 0;
        var tail = '';
        if (iod > 0) {// with decimal
            rstr = numstr.substring(0, iod);
            decstr = numstr.substring(iod + 1, numstr.length);
            while (tail.length < roundnum) {
                if (decstr.charAt(i)) {
                    tail += decstr.charAt(i);
                } else {
                    tail += "0";
                }
                i++;
            }
            decstr = tail;
        } else {// no decimal
            rstr = numstr;
            while (decstr.length < roundnum) {
                decstr += "0";
            }
        }
        return(rstr + "." + decstr);
    }
    </script>
    </head>
    <body>
    <input type="button" value="Check" onclick="alert(round2(3.213))" />
    </body>
    </html>
    
    HTML:
     
    xrvel, Apr 30, 2008 IP
  3. pdorais75

    pdorais75 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't understand how to incorporate the round calculation in my original script for have a result with only 2 decimal.

    Can you copy past my script and add the line for make it work?
     
    pdorais75, Apr 30, 2008 IP
  4. roshanbh

    roshanbh Peon

    Messages:
    52
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    var number=2489.8237;
    alert(profits.toFixed(2)); outputs 2489.82

    hope this helps
     
    roshanbh, May 1, 2008 IP
    xrvel likes this.
  5. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #5
    Nice, i didn't know about that :) Repped :)
     
    xrvel, May 1, 2008 IP
  6. pdorais75

    pdorais75 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry:confused:, no, i'm not a pro in javascript. I just start understanding this JS :eek:....Where i exactly paste the code in my html page for make it work???

    I put here the complete code of the html page, show m where to copy the lines.

    Thanks for help.

    <html>
    <head> 
    
    <script language="javascript">
    function calc (form) {
    form.liter.value =
    form.length.value * 
    form.width.value *
    form.height.value *0.016387064069264;
    form.gallon.value =
    form.liter.value /100 *26.4172; 
    } 
    </script> 
    
    </head>
    <body> 
    <form> 
    
    length: <input type="text" class="but" name="length" size="2" maxlength="3"><br>
    width: <input type="text" class="but" name="width" size="2" maxlength="3"><br>
    height: <input type="text" class="but" name="height" size="2" maxlength="3"><br><br>
    
    <input type="button" class="bouton" value="GO" onclick="calc(this.form)"> 
    
    <br><br>
    Liters: <input type="text" SIZE="8" value="" name="liter"><br>
    Gallon: <input type="text" SIZE="8" value="" name="gallon"><br>
    
    
    </form> 
    </body>
    </html> 
    Code (markup):
     
    pdorais75, May 2, 2008 IP
  7. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Which one are you going to round-off?
     
    Dondon2d, May 2, 2008 IP
  8. pdorais75

    pdorais75 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If possible, i like to round off the 2 answers, i like to see only 2 digit in the decimal answer.
     
    pdorais75, May 3, 2008 IP
  9. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Change this:

    <script language="javascript">
    function calc (form) {
    form.liter.value =
    form.length.value * 
    form.width.value *
    form.height.value *0.016387064069264;
    form.gallon.value =
    form.liter.value /100 *26.4172; 
    } 
    </script> 
    Code (markup):
    To this:

    <script language="javascript">
    function calc (form)
    {
        var Liters          = form.length.value * form.width.value * form.height.value * 0.016387064069264;
        var Gallons         = Liters * 0.264172;
        form.liter.value    = Liters.toFixed(2);
        form.gallon.value   = Gallons.toFixed(2);
    }
    </script>
    Code (markup):
     
    vpguy, May 3, 2008 IP
  10. pdorais75

    pdorais75 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Wow, now it's work perfectly:):)....Thak you.

    I will be ble to study the chage now.

    thank again.:D
     
    pdorais75, May 3, 2008 IP