C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Javascript function to convert amount in words


Posted Date: 12 Sep 2008    Resource Type: Code Snippets    Category: Javascript
Author: Shivashankar ChincholiMember Level: Gold    
Rating: Points: 10



/* FUNCTION TO CONVERT AMOUNT IN WORDS */


function amtInWords()
{

var decAmount=document.getElementById("sAmount2").value;
var sUnits=new Array(20);
var sTens=new Array(8);
var sHundreds=new Array(6);
var sAmount;
var i,iLenAmount,iDecPart,iIntegerPart;

sUnits[1] = '';
sUnits[2] = 'One';
sUnits[3] = 'Two';
sUnits[4] = 'Three';
sUnits[5] = 'Four';
sUnits[6] = 'Five';
sUnits[7] = 'Six';
sUnits[8] = 'Seven';
sUnits[9] = 'Eight';
sUnits[10] = 'Nine';
sUnits[11] = 'Ten';
sUnits[12] = 'Eleven';
sUnits[13] = 'Twelve';
sUnits[14] = 'Thirteen';
sUnits[15] = 'Fourteen';
sUnits[16] = 'Fifteen';
sUnits[17] = 'Sixteen';
sUnits[18] = 'Seventeen';
sUnits[19] = 'Eighteen';
sUnits[20] = 'Ninteen';
sTens[1] = 'Twenty';
sTens[2] = 'Thirty';
sTens[3] = 'Forty';
sTens[4] = 'Fifty';
sTens[5] = 'Sixty';
sTens[6] = 'Seventy';
sTens[7] = 'Eighty';
sTens[8] = 'Ninety';
sHundreds[1] = 'Hundred';
sHundreds[2] = 'Thousand';
sHundreds[3] = 'Lac';
sHundreds[4] = 'Crore';
sHundreds[5] = 'Arab';
sHundreds[6] = 'Kharab';

if (decAmount == 10000000000000)
{
decAmount = 9999999999999.99;
}
if (decAmount == 0)
{
return "";
}

iDecPart = (decAmount - Math.round(decAmount)) * 100;
iDecPart=Math.round(iDecPart);

//Because Math.round results .50,.52,.53.......98,.99 in negative values

if(iDecPart<0)
{
iDecPart=100+iDecPart;
}

if( iDecPart == 0)
{
decAmount = decAmount;
}
else
{
decAmount =Math.round(decAmount - (iDecPart/100));
}

iLenAmount = ((String)(decAmount)).length;

if (iLenAmount == 1)
{
var index=parseInt(decAmount)+1;
sAmount = sUnits[index];
}
else
{
for(i=iLenAmount;i>0;i--)
{
if (i==13 || i==12)
{
iIntegerPart = parseInt(decAmount/100000000000);
decAmount = parseInt(decAmount % 100000000000);
if(iIntegerPart==0)
{
sAmount = sAmount;
}
else
{
if(iIntegerPart<20)
{
sAmount = sUnits[iIntegerPart + 1] +" "+ sHundreds[6]+" ";
}
else
{
sAmount = sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1] +" "+ sHundreds[6]+" "
}
}
}
else if (i==11 || i==10)
{
iIntegerPart = parseInt(decAmount/1000000000);
decAmount = parseInt(decAmount % 1000000000);
if(iIntegerPart==0)
{
sAmount = sAmount;
}
else
{
if(iIntegerPart<20)
{
if(sAmount == null)
{
sAmount = sUnits[iIntegerPart + 1] +" "+ sHundreds[5]+" ";
}
else
{
sAmount = sAmount+" "+sUnits[iIntegerPart + 1] +" "+ sHundreds[5]+" ";
}
}
else
{
if(sAmount == null)
{
sAmount = sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1] +" "+ sHundreds[5]+" ";
}
else
{
sAmount = sAmount+" "+sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1] +" "+ sHundreds[5]+" ";
}
}
}
}
else if (i==9 || i==8)
{
iIntegerPart = parseInt(decAmount/10000000);
decAmount = parseInt(decAmount % 10000000);
if(iIntegerPart==0)
{
sAmount = sAmount;
}
else
{
if(iIntegerPart<20)
{
if(sAmount == null)
{
sAmount = sUnits[iIntegerPart + 1] +" "+ sHundreds[4]+" ";
}
else
{
sAmount = sAmount+" "+sUnits[iIntegerPart + 1] +" "+ sHundreds[4]+" ";
}
}
else
{
if(sAmount == null)
{
sAmount = sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1] +" "+ sHundreds[4]+" ";
}
else
{
sAmount = sAmount+" "+sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1] +" "+ sHundreds[4]+" ";
}
}
}
}
else if(i==7 || i==6)
{
iIntegerPart = parseInt(decAmount/100000);
decAmount = (decAmount % 100000);
if(iIntegerPart==0)
{
sAmount = sAmount;
}
else
{
if(iIntegerPart < 20)
{
if(sAmount == null)
{
sAmount =sUnits[iIntegerPart + 1]+" "+ sHundreds[3]+" ";
}
else
{
sAmount = sAmount+" "+sUnits[iIntegerPart + 1]+" "+ sHundreds[3]+" ";
}
}
else
{
if(sAmount == null)
{
sAmount = sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1] +" "+ sHundreds[3]+" ";
}
else
{
sAmount = sAmount+" "+sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1] +" "+ sHundreds[3]+" ";
}
}
}
}
else if(i==5 || i==4)
{
iIntegerPart = parseInt(decAmount/1000);
decAmount = (decAmount % 1000);
if(iIntegerPart==0)
{
sAmount = sAmount;
}
else
{
if(iIntegerPart < 20)
{
if(sAmount == null)
{
sAmount = sUnits[iIntegerPart + 1]+" "+ sHundreds[2]+" ";
}
else
{
sAmount = sAmount+" "+sUnits[iIntegerPart + 1]+" "+ sHundreds[2]+" ";
}
}
else
{
if(sAmount == null)
{
sAmount = sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1]+" "+ sHundreds[2]+" ";
}
else
{
sAmount = sAmount+" "+sTens[parseInt(iIntegerPart/10) - 1] +" "+ sUnits[(iIntegerPart - parseInt(iIntegerPart/10)*10) + 1]+" "+ sHundreds[2]+" ";
}
}
}
}
else if(i==3)
{
iIntegerPart = parseInt(decAmount/100);
decAmount = (decAmount % 100);
if(iIntegerPart==0)
{
sAmount = sAmount;
}
else
{
var index;
index=parseInt(iIntegerPart)+1;
if (sAmount == null)
{
sAmount = sUnits[index] +" "+ sHundreds[1]+" ";
}
else
{
sAmount = sAmount+" "+sUnits[index] +" "+ sHundreds[1]+" ";
}
}
}
else if(i==2)
{
decAmount=parseInt(eval(decAmount));
if(decAmount<20)
{
var index=parseInt(decAmount)+1;
if (sAmount == null)
{
sAmount = sUnits[index];
}
else
{
sAmount = sAmount+" "+sUnits[index];
}
}
else
{
var a=parseInt(((decAmount/10) - 1));
var b=(decAmount%10) + 1;
if (sAmount == null)
{
sAmount = sTens[a] +" "+ sUnits[b];
}
else
{
sAmount = sAmount+" "+sTens[a] +" "+ sUnits[b];
}
}
}
}
}
if(iDecPart==0)
{
sAmount = "Rs. " + sAmount;
}
else if(sAmount=="")
{
sAmount = "Paise ";
}
else
{
sAmount = "Rs. "+sAmount+" And Paise";
}

if(iDecPart < 20)
{
sAmount = sAmount+" "+sUnits[iDecPart + 1]+" ";
}
else
{
var fi = parseInt(((iDecPart/10) - 1));
var fii = parseInt((iDecPart % 10))+1;
sAmount = sAmount+" "+sTens[fi] +" "+ sUnits[fii]+" " ;
}

sAmount = sAmount + " Only";

return sAmount;

}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Javascript function to convert amount in words  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Javascript code to adds no of years to a date
Previous Resource: Javascript code for Stopping the Ctrl+V key on controls
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use