...

Round Off a number function

jalarie
07-22-2002, 02:22 PM
To round off a value to a specified number of digits:

function RoundIt(Value,Digits) {
 var P=Math.pow(10,Digits);
 var R=Math.round(Value*P)/P;
 var G=true;
 while(G) {
  R=String(R);
  var L=R.length;
  var I=R.indexOf('.');
  if (I == -1) {
   R=R+'.';
  } else {
   var D=L-I-1;
&nbsp;&nbsp;&nbsp;if (D < Digits) {
&nbsp;&nbsp;&nbsp;&nbsp;R=R+'0';
&nbsp;&nbsp;&nbsp;} else {
&nbsp;&nbsp;&nbsp;&nbsp;G=false;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;}
&nbsp;return R;
}

jkd
07-22-2002, 06:04 PM
5.2344573.toFixed(2) == "5.23"

toFixed was introduced in IE5.5 and NS6.

jalarie
07-23-2002, 05:56 PM
That's very nice for the IE5.5 and NS6 people, but there are still quite a few who haven't gotten that far, many who are using other browsers by choice, and some visually-impaired who are forced to use other browsers out of necessity.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum