Mega Code Archive

 
Categories / JavaScript DHTML / Development
 

Number format, round and total

<HTML> <HEAD> <SCRIPT LANUAGE="JavaScript1.1"> // returns the amount in the .99 format function twoPlaces(amount) {   return (amount == Math.floor(amount)) ? amount + '.00' : ((amount*10 == Math.floor(amount*10)) ? amount + '0' : amount); }   // rounds number to X decimal places, defaults to 2 function round(number,X) {   X = (!X ? 2 : X);   return Math.round(number*Math.pow(10,X))/Math.pow(10,X); } function totals(num) {   return twoPlaces(Math.floor((num - 0) * 100) / 100); } </SCRIPT> </HEAD> <BODY > <B> Two Decimal Places </B> <BR> 0.00 is  <script LANUAGE="JavaScript1.1"> document.write(twoPlaces(0.00)); </script> <BR> .10 is  <script LANUAGE="JavaScript1.1"> document.write(twoPlaces(.10)); </script> <BR> 1 is  <script LANUAGE="JavaScript1.1"> document.write(twoPlaces(1)) </script> <BR> .-530 is  <script LANUAGE="JavaScript1.1"> document.write(twoPlaces(-.530) ) </script> <BR><BR>  51.02 - 3.8 =  <B> <script LANUAGE="JavaScript1.1"> document.write(round(totals(51.02) - totals(3.80) )); </script> </BODY> </HTML>