473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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!

Expand|Select|Wrap|Line Numbers
  1. <script language="Javascript">
  2.     function sum()
  3.         {
  4.             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));
  5.             return false;
  6.         }
  7.  
  8. function FP_callJS() {//v1.0
  9.  eval(arguments[0]);
  10. }
  11. </script>
  12.  
Mar 18 '08 #1
20 6644
gits
5,390 Expert Mod 4TB
hi ...

you may use:

Expand|Select|Wrap|Line Numbers
  1. var val = 58.567;
  2.  
  3. // round val to two decimals
  4. var res = Math.round(val*100)/100;
  5.  
  6. // res is now 58.57
kind regards
Mar 18 '08 #2
hi ...

you may use:

Expand|Select|Wrap|Line Numbers
  1. var val = 58.567;
  2.  
  3. // round val to two decimals
  4. var res = Math.round(val*100)/100;
  5.  
  6. // res is now 58.57
kind regards
Sorry for being a pain but could you show me in my code above where to put this? Thanks.
Mar 18 '08 #3
gits
5,390 Expert Mod 4TB
let me give you an example for how to do that :)

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function get_sum() {
  3.     var frm  = document.form1;
  4.     var outp = frm.Text155.value;
  5.     var res  = 0;
  6.  
  7.     for (var i = 2; i < 7; i++) {
  8.         res += parseInt(frm['Text5' + i].value, 10);
  9.     }
  10.  
  11.     outp = Math.round(res*100)/100;
  12.  
  13.     return false;
  14. }
  15. </script>
  16.  
kind regards
Mar 18 '08 #4
Got it...you're the best. Thank you.
Mar 18 '08 #5
gits
5,390 Expert Mod 4TB
no problem ... glad to hear you got it working ... post back to the forum anytime you have more questions :)

kind regards
Mar 18 '08 #6
acoder
16,027 Expert Mod 8TB
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.
Mar 19 '08 #7
hback
7
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:

Expand|Select|Wrap|Line Numbers
  1. <form>
  2.                   <table width="98%" border="0" cellpadding="0" cellspacing="0">
  3.                     <tr>
  4.                       <td width="36%"><div align="right">Degrees F:&nbsp;</FONT>
  5.                           <input type="text" name="F" value="32"
  6.     onchange="C.value = 100/(212-32) * (this.value - 32 )" />
  7.                       </div></td>
  8.                       <td width="43%" align="right">to Degrees C:&nbsp;</FONT>
  9.                         <input name="C" type="text"
  10.     onchange="F.value = (212-32)/100 * this.value + 32 " value="0.0" /></td>
  11.                       <td width="21%">&nbsp;</td>
  12.                     </tr>
  13.                   </table>
  14.                   </form>
Any suggestions would be much appreciated.
Mar 11 '09 #8
gits
5,390 Expert Mod 4TB
in case you always want to display the 2 decimals, then you may use the toFixed() method ...

kind regards
Mar 12 '09 #9
hback
7
Thank you for your reply. Where do I paste this? ...and do I paste this entire block?

Expand|Select|Wrap|Line Numbers
  1. var n = 12345.6789;
  2.  
  3. n.toFixed();              // Returns 12346: note rounding, no fractional part
  4. n.toFixed(1);             // Returns 12345.7: note rounding
  5. n.toFixed(6);             // Returns 12345.678900: note added zeros
  6. (1.23e+20).toFixed(2);    // Returns 123000000000000000000.00
  7. (1.23e-10).toFixed(2)     // Returns 0.00
Thank You!
Mar 12 '09 #10
Dormilich
8,658 Expert Mod 8TB
you do not paste this code block. that's just for demonstration how to use it.

another example:
Expand|Select|Wrap|Line Numbers
  1. var n = 10.234;
  2. var x = n.toFixed(2);   // x is now 10.23
Mar 12 '09 #11
hback
7
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.
Mar 12 '09 #12
gits
5,390 Expert Mod 4TB
you need to do something like this:

Expand|Select|Wrap|Line Numbers
  1. F.value = ((212-32)/100 * this.value + 32).toFixed(2);
Mar 12 '09 #13
hback
7
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.


Expand|Select|Wrap|Line Numbers
  1. <form>
  2.                   <table width="98%" border="0" cellpadding="0" cellspacing="0">
  3.                     <tr>
  4.                       <td width="36%"><div align="right">Degrees F:&nbsp;</FONT>
  5.                           <input type="text" name="F" value="32"
  6.     onchange="C.value = 100/(212-32) * (this.value - 32 ) .toFixed(2);" />
  7.                       </div></td>
  8.                       <td width="43%" align="right">to Degrees C:&nbsp;</FONT>
  9.                         <input name="C" type="text"
  10.     onchange="F.value = ((212-32)/100 * this.value + 32).toFixed(2);"  /></td>
  11.                       <td width="21%">&nbsp;</td>
  12.                     </tr>
  13.                   </table>
  14.                   </form>
Mar 12 '09 #14
gits
5,390 Expert Mod 4TB
is it in the C-field (line 6 in your above code)? you need to set the brackets like in the example ...

kind regards
Mar 12 '09 #15
hback
7
Ok some of this worked. It works from C Degrees to F Degrees, but not the other way around. Could you tell me what I'm doing wrong in the first half of this code?

Expand|Select|Wrap|Line Numbers
  1. <form>
  2.                   <table width="98%" border="0" cellpadding="0" cellspacing="0">
  3.                     <tr>
  4.                       <td width="36%"><div align="right">Degrees F:&nbsp;</FONT>
  5.                           <input type="text" name="F" value="32"
  6.     onchange="C.value = 100/(212-32) * (this.value - 32).toFixed(2); " />
  7.                       </div></td>
  8.                       <td width="43%" align="right">to Degrees C:&nbsp;</FONT>
  9.                         <input name="C" type="text"
  10.     onchange="F.value = ((212-32)/100 * this.value + 32).toFixed(2); " value="0.0" /></td>
  11.                       <td width="21%">&nbsp;</td>
  12.                     </tr>
  13.                   </table>
  14.                   </form>
Thank You!
Mar 12 '09 #16
gits
5,390 Expert Mod 4TB
as i said, the brackets are the problem:

Expand|Select|Wrap|Line Numbers
  1. C.value = (100/(212-32) * (this.value - 32)).toFixed(2);
Mar 12 '09 #17
hback
7
Thank You! It works perfectly. I appreciate your time and patience with me. I wasn't sure where to put the second bracket on the first half of the code.
Mar 12 '09 #18
gits
5,390 Expert Mod 4TB
as you should see now, that you have to set the brackets according to the value that you want to have 'Fixed' ... the toFixed() method is used on numeric-values and casts the value to a string with the passed number of decimals ...
Mar 12 '09 #19
hback
7
I understand now. Thank you for your time. You are a genius!
Mar 12 '09 #20
gits
5,390 Expert Mod 4TB
nope ;) ... but in case you have more questions just post back to the forum ;)

kind regards
Mar 12 '09 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Joe M Blow | last post by:
Hi evreyone I just want to kno how to make my value (a float) and round it off to two decimal places Thanks alot Ty
3
by: Brent Bortnick | last post by:
Does anyone know how to find out the number of decimal places a number has. I need this info so that i can round to 3 decimal places if the number has 3 decimal places or to 2 decimal places if...
1
by: Gizmo | last post by:
Hi there i was wondering if any one new to a function that rounds up a float to so many decimal places. I have a number in bytes and converting it to mb's and gb's but once its converted i need to...
6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
4
by: Ron | last post by:
Greetings, int i = 1, j = 6; double k = (double)i/j; Console.WriteLine(k.ToString()); Console.WriteLine(string.Format(k.ToString(), "0.00")); both yield 0.166666666666667 how can I make...
6
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
10
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I format a Number as a String with exactly 2 decimal places?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.