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:
- <!--Author : Mohammad Usman
- Date : 10/03/2011 -->
- <html>
- <script language="javascript">
- function numberformat(){
- Number.prototype.toCurrency=function(noFractions,currencySymbol,decimalSeparator,thousandsSeparator){
- var n,startAt,intLen;
- if (currencySymbol==null) currencySymbol="$";
- if (decimalSeparator==null) decimalSeparator=".";
- if (thousandsSeparator==null) thousandsSeparator=",";
- n = this.round(noFractions?0:2,true,decimalSeparator);
- intLen=n.length-(noFractions?0:3);
- if ((startAt=intLen%3)==0) startAt=3;
- for (var i=0,len=Math.ceil(intLen/3)-1;i<len;i++)n=n.insertAt(i*4+startAt,thousandsSeparator);
- return currencySymbol+n;
- }
- Number.prototype.toInteger=function(thousandsSeparator){
- var n,startAt,intLen;
- if (thousandsSeparator==null) thousandsSeparator=",";
- n = this.round(0,true);
- intLen=n.length;
- if ((startAt=intLen%3)==0) startAt=3;
- for (var i=0,len=Math.ceil(intLen/3)-1;i<len;i++)n=n.insertAt(i*4+startAt,thousandsSeparator);
- return n;
- }
- Number.prototype.round=function(decimals,returnAsString,decimalSeparator){
- //Supports 'negative' decimals, e.g. myNumber.round(-3) rounds to the nearest thousand
- var n,factor,breakPoint,whole,frac;
- if (!decimals) decimals=0;
- factor=Math.pow(10,decimals);
- n=(this.valueOf()+""); //To get the internal value of an Object, use the valueOf() method
- if (!returnAsString) return Math.round(n*factor)/factor;
- if (!decimalSeparator) decimalSeparator=".";
- if (n==0) return "0."+((factor+"").substr(1));
- breakPoint=(n=Math.round(n*factor)+"").length-decimals;
- whole = n.substr(0,breakPoint);
- if (decimals>0){
- frac = n.substr(breakPoint);
- if (frac.length<decimals) frac=(Math.pow(10,decimals-frac.length)+"").substr(1)+frac;
- return whole+decimalSeparator+frac;
- }else return whole+((Math.pow(10,-decimals)+"").substr(1));
- }
- String.prototype.insertAt=function(loc,strChunk){
- return (this.valueOf().substr(0,loc))+strChunk+(this.valueOf().substr(loc))
- }
- var quantity=1056;
- var costPer=3.9;
- var totalCost=quantity*costPer;
- alert(quantity.toInteger()+" items at "+costPer.toCurrency()+" per item amounts to a total of "+totalCost.toCurrency());
- //Yields "1,056 items at $3.90 per item amounts to a total of $4,118.40"
- }
- </script>
- </head>
- <body>
- <input type="button" value="Example of Number Format" onclick="numberformat();"/>
- </html>
No comments:
Post a Comment