Number Formatting in Javascript

Number formatting is very typical process in every programming language , here ,I have a solution of this type of problem , which is written in javascript language , in these lines of code ,you can convert any type of number as you want like "1,056 items at $3.90 per item amounts to a total of $4,118.40" this type of number conversion , now question arise here that ,will it run on every language like ASP , JSP....etc ?
again answer is same yes always , before trying this I recommend that get a little bit knowledge of prototype of  javascript language it will help you to understand block of code. Click here to Download Demo .


Code:
  1. <!--Author : Mohammad Usman
  2.    Date   : 10/03/2011 -->
  3. <head><title>Number Formatting</title>
  4. <script language="javascript">
  5. function numberformat(){
  6. Number.prototype.toCurrency=function(noFractions,currencySymbol,decimalSeparator,thousandsSeparator){
  7.    var n,startAt,intLen;
  8.    if (currencySymbol==null) currencySymbol="$";
  9.    if (decimalSeparator==null) decimalSeparator=".";
  10.    if (thousandsSeparator==null) thousandsSeparator=",";
  11.    n = this.round(noFractions?0:2,true,decimalSeparator);
  12.    intLen=n.length-(noFractions?0:3);
  13.    if ((startAt=intLen%3)==0) startAt=3;
  14.    for (var i=0,len=Math.ceil(intLen/3)-1;i<len;i++)n=n.insertAt(i*4+startAt,thousandsSeparator);
  15.   return currencySymbol+n;
  16. }
  17. Number.prototype.toInteger=function(thousandsSeparator){
  18.   var n,startAt,intLen;
  19.   if (thousandsSeparator==null) thousandsSeparator=",";
  20.   n = this.round(0,true);
  21.   intLen=n.length;
  22.   if ((startAt=intLen%3)==0) startAt=3;
  23.   for (var i=0,len=Math.ceil(intLen/3)-1;i<len;i++)n=n.insertAt(i*4+startAt,thousandsSeparator);
  24.   return n;
  25. }
  26. Number.prototype.round=function(decimals,returnAsString,decimalSeparator){
  27.   //Supports 'negative' decimals, e.g. myNumber.round(-3) rounds to the nearest thousand
  28.   var n,factor,breakPoint,whole,frac;
  29.   if (!decimals) decimals=0;
  30.   factor=Math.pow(10,decimals);
  31.   n=(this.valueOf()+"");         //To get the internal value of an Object, use the valueOf() method
  32.   if (!returnAsString) return Math.round(n*factor)/factor;
  33.   if (!decimalSeparator) decimalSeparator=".";
  34.   if (n==0) return "0."+((factor+"").substr(1));
  35.   breakPoint=(n=Math.round(n*factor)+"").length-decimals;
  36.   whole = n.substr(0,breakPoint);
  37.   if (decimals>0){
  38.       frac = n.substr(breakPoint);
  39.       if (frac.length<decimals) frac=(Math.pow(10,decimals-frac.length)+"").substr(1)+frac;
  40.      return whole+decimalSeparator+frac;
  41.   }else return whole+((Math.pow(10,-decimals)+"").substr(1));
  42. }
  43. String.prototype.insertAt=function(loc,strChunk){
  44.   return (this.valueOf().substr(0,loc))+strChunk+(this.valueOf().substr(loc))
  45. }
  46. var quantity=1056;
  47. var costPer=3.9;
  48. var totalCost=quantity*costPer;
  49. alert(quantity.toInteger()+" items at "+costPer.toCurrency()+" per item amounts to a total of "+totalCost.toCurrency());
  50. //Yields "1,056 items at $3.90 per item amounts to a total of $4,118.40"
  51. }
  52. </head>
  53. <input type="button" value="Example of Number Format" onclick="numberformat();"/>
  54. </html>


No comments:

Post a Comment

Comment On Facebook