Number format, round and total : Number Format : Development JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » Development » Number Format »

 

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 ? : X);
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function totals(num) {
  return twoPlaces(Math.floor((num - 0100100);
}

</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>
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>

           
       



-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo Development
» Number Format