Convert any number into USD

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apurvaG
    New Member
    • Apr 2007
    • 14

    Convert any number into USD

    Hi All,

    I want to convert any number into USD using action script 2. It should add the dollar sign and appropriate comma's to the input number.

    Thanks in advacne,
    AG
  • gulyan
    New Member
    • Aug 2008
    • 11

    #2
    try this function:

    Code:
    function NumberToUSD(input)
    {
    	var nr = new Number(input);
    	var str = new String();
    	str = nr.toString();
    	var a = str.indexOf('.');
    	var p2 = str.substr(a, str.length) + '$';
    	var p1 = str.substr(0, a);
    	a = p1.length;
    	switch (a%3)
    	{
    		case 0:break;
    		case 1:str = p1.substr(0,1);p1 = p1.substr(1,p1.length-1);break;
    		case 2:str = p1.substr(0,2);p1 = p1.substr(2,p1.length-2);break;
    	}
    	while(p1.length)
    	{
    		str += "," + p1.substr(0,3);
    		p1 = p1.substr(3,p1.length-3);
    	}
    	str += p2;
    	return str;
    }

    Comment

    Working...