Round to two decimal places

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jalley06
    New Member
    • Mar 2008
    • 20

    #1

    Round to two decimal places

    Update: The below code is now working but I need a way to round the value in Text155 to only two decimal places. Can someone help?

    Hi, I have a javascript function that is returning NaN in Text155 field. Can someone tell me what I'm doing wrong? If you haven't already guessed, I am also new to the Javascript world. Thanks!

    Code:
    <script language="Javascript">
    	function sum()
    		{
    			document.form1.Text155.value = (parseInt(document.form1.Text52.value) + parseInt(document.form1.Text53.value) + parseInt(document.form1.Text54.value) + parseInt(document.form1.Text55.value) + parseInt(document.form1.Text56.value));
    			return false;
    		}
    
    function FP_callJS() {//v1.0
     eval(arguments[0]);
    }
    </script>
    Last edited by jalley06; Mar 18 '08, 03:55 PM. Reason: Update:
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    you may use:

    [CODE=javascript]var val = 58.567;

    // round val to two decimals
    var res = Math.round(val* 100)/100;

    // res is now 58.57[/CODE]
    kind regards

    Comment

    • jalley06
      New Member
      • Mar 2008
      • 20

      #3
      Originally posted by gits
      hi ...

      you may use:

      [CODE=javascript]var val = 58.567;

      // round val to two decimals
      var res = Math.round(val* 100)/100;

      // res is now 58.57[/CODE]
      kind regards
      Sorry for being a pain but could you show me in my code above where to put this? Thanks.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        let me give you an example for how to do that :)

        [CODE=javascript]<script type="text/javascript">
        function get_sum() {
        var frm = document.form1;
        var outp = frm.Text155.val ue;
        var res = 0;

        for (var i = 2; i < 7; i++) {
        res += parseInt(frm['Text5' + i].value, 10);
        }

        outp = Math.round(res* 100)/100;

        return false;
        }
        </script>
        [/CODE]
        kind regards

        Comment

        • jalley06
          New Member
          • Mar 2008
          • 20

          #5
          Got it...you're the best. Thank you.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            no problem ... glad to hear you got it working ... post back to the forum anytime you have more questions :)

            kind regards

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              hbella, you posted this using the Report button:
              hi gits
              i need to change fields into R 000,000,000.00 but for the display, data
              patterns i use ($Z,Z9.99) but after execution it allows/ outputs only
              four digits.pls help!!
              greeting and thanx
              Please use the Reply link/button in future (the one to the far right). Thanks.

              Comment

              • hback
                New Member
                • Mar 2009
                • 7

                #8
                I am very new to this. I'm working on a website: http://www.nhtpc.com/tools.htm where i'm trying to get the calculations to round to 2 decimal places where as right now it's showing 10.

                Below is the code i'm using:

                Code:
                <form>
                                  <table width="98%" border="0" cellpadding="0" cellspacing="0">
                                    <tr>
                                      <td width="36%"><div align="right">Degrees F:&nbsp;</FONT>
                                          <input type="text" name="F" value="32"
                    onchange="C.value = 100/(212-32) * (this.value - 32 )" />
                                      </div></td>
                                      <td width="43%" align="right">to Degrees C:&nbsp;</FONT>
                                        <input name="C" type="text"
                    onchange="F.value = (212-32)/100 * this.value + 32 " value="0.0" /></td>
                                      <td width="21%">&nbsp;</td>
                                    </tr>
                                  </table>
                                  </form>
                Any suggestions would be much appreciated.
                Last edited by Dormilich; Mar 11 '09, 09:45 PM. Reason: added [code] tags

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  in case you always want to display the 2 decimals, then you may use the toFixed() method ...

                  kind regards

                  Comment

                  • hback
                    New Member
                    • Mar 2009
                    • 7

                    #10
                    Thank you for your reply. Where do I paste this? ...and do I paste this entire block?

                    Code:
                    var n = 12345.6789;
                    
                    n.toFixed();              // Returns 12346: note rounding, no fractional part
                    n.toFixed(1);             // Returns 12345.7: note rounding
                    n.toFixed(6);             // Returns 12345.678900: note added zeros
                    (1.23e+20).toFixed(2);    // Returns 123000000000000000000.00
                    (1.23e-10).toFixed(2)     // Returns 0.00
                    Thank You!
                    Last edited by Dormilich; Mar 12 '09, 05:03 PM. Reason: added [code] tags

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      you do not paste this code block. that's just for demonstration how to use it.

                      another example:
                      Code:
                      var n = 10.234;
                      var x = n.toFixed(2);   // x is now 10.23

                      Comment

                      • hback
                        New Member
                        • Mar 2009
                        • 7

                        #12
                        I greatly appreciate everyone who has posted for this topic, but I don't even know enough to know what to do with that.

                        Thanks for your time.

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5390

                          #13
                          you need to do something like this:

                          Code:
                          F.value = ((212-32)/100 * this.value + 32).toFixed(2);

                          Comment

                          • hback
                            New Member
                            • Mar 2009
                            • 7

                            #14
                            Thank You gits..

                            I believe I am still doing something wrong. It not rounding up, and it's still giving me lots of numberes after the decimal. Please see code below.


                            Code:
                            <form>
                                              <table width="98%" border="0" cellpadding="0" cellspacing="0">
                                                <tr>
                                                  <td width="36%"><div align="right">Degrees F:&nbsp;</FONT>
                                                      <input type="text" name="F" value="32"
                                onchange="C.value = 100/(212-32) * (this.value - 32 ) .toFixed(2);" />
                                                  </div></td>
                                                  <td width="43%" align="right">to Degrees C:&nbsp;</FONT>
                                                    <input name="C" type="text"
                                onchange="F.value = ((212-32)/100 * this.value + 32).toFixed(2);"  /></td>
                                                  <td width="21%">&nbsp;</td>
                                                </tr>
                                              </table>
                                              </form>
                            Last edited by gits; Mar 12 '09, 06:38 PM. Reason: added code tags

                            Comment

                            • gits
                              Recognized Expert Moderator Expert
                              • May 2007
                              • 5390

                              #15
                              is it in the C-field (line 6 in your above code)? you need to set the brackets like in the example ...

                              kind regards

                              Comment

                              Working...