round in javascript

1. Senior Member
Join Date
Dec 1969
Posts
272

## round in javascript

Hi all!<BR><BR>I am using this function to do some calculations to get the <BR>ROI <BR><BR>function Check()<BR>{ <BR> var oForm = document.MyForm; <BR>oForm.TF.value= oForm.txtCTE2H3.value<BR>oForm.TI.value=(parseFloa t(oForm.TF.value) + parseFloat(oForm.ITI.value))<BR> <BR> oForm.ROI.value = (parseFloat(oForm.RS.value) / (parseFloat(oForm.txtCTE2H3.value) + parseFloat(oForm.ITI.value))); <BR> if(isNaN(oForm.ROI.value)){ oForm.ROI.value = 0; } <BR><BR>}<BR><BR>Now the value it calculates should be rounded of to 2 decimals if the value is 23.79789797 it should be 23.80<BR><BR>what is the equivalant of round in javascript

2. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Try irt.org.

They have all kinds of FAQs - I&#039;d guess this is answered in one of them.<BR><BR>http://www.irt.org/

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## You have to write your own..

JS has Math.round() but that only rounds to zero decimal places.<BR><BR>I don&#039;t like the versions that www.irt.org offers.<BR><BR>Here&#039;s my own:<BR><BR>function round2( num )<BR>{<BR> num = parseFloat( num ) &#039; insurance<BR> var sign = "";<BR> if ( num &#060; 0 ) <BR> {<BR> sign = "-";<BR> num = - num;<BR> }<BR> num = Math.round( 100 * num );<BR> if ( num &#060; 10 ) return sign + "0.0" + num;<BR> if ( num &#060; 100 ) return sign + "0." + num;<BR> num = sign + num; // converts num to a string<BR> return num.substring(0,num.length-2) + "." + num.substring(num.length-2);<BR>}<BR><BR>Disadvantage: You have to have a different function for each number of digits you want rounding to. <BR><BR>Advantage: Much simpler, much faster, and how often do you need other than 2 digits anyway?<BR><BR>

#### Posting Permissions

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