ASP.NET - Format Number - Asked By arvind gupta on 13-Feb-12 09:18 AM

User enters 1234567891234 and the format would be $ 1,234,567,891,234  using javascript

D Company replied to arvind gupta on 13-Feb-12 09:24 AM
Hello Arvind,

here is a java script function to do this,

function ChnagetoDollar(NumberPassed)
{
NumberPassed= NumberPassed.toString().replace(/\$|\,/g,'');
if(isNaN(NumberPassed))
NumberPassed= "0";
sign = (NumberPassed== (NumberPassed= Math.abs(NumberPassed)));
NumberPassed= Math.floor(NumberPassed*100+0.50000000001);
cents = NumberPassed%100;
NumberPassed= Math.floor(NumberPassed/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
NumberPassed= NumberPassed.substring(0,NumberPassed.length-(4*i+3))+','+
NumberPassed.substring(NumberPassed.length-(4*i+3));
return (((sign)?'':'-') + '$' + NumberPassed+ '.' + cents);
}


Hope it helps!!
Regards
D
Riley K replied to arvind gupta on 13-Feb-12 09:55 AM


I found a jQuery currency formatter plugin which would be very easy for you to implement


You can simply specify like this 

$('.label').formatCurrency();


refer this link for download 

http://code.google.com/p/jquery-formatcurrency/downloads/list 

http://code.google.com/p/jquery-formatcurrency/wiki/Usage 


Regards
Web Star replied to arvind gupta on 13-Feb-12 10:57 AM
You can just use this javascript function to  format number in currency format as follows

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}