You need to sign in to do that
Don't have an account?
Currency Number to Word
I was looking for a number to word code for apex today and to my disappointment, the only think I could find was a paid app on appexchange http://appexchange.salesforce.com/listingDetail?listingId=a0N300000059Vz6EAE
which I though was ridiculous, so I created my own, its not perfect, but it works and I wanted to share it in the community.
public static string wtn(Decimal i, String curr)
{
String text='';
String bigD='', smallD='';
Decimal Bn=0, Gn=0, Kn=0, Hn=0, Dn=0, tDn=0, dec=0;
Integer n=0;
String[] ones = new String[]{'', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eightteen',
'Nineteen'};
String[] tens = new String[]{'', 'Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty',
'Seventy', 'Eigthy', 'Ninety'};
//More currencies can be added
if(curr == 'USD'){ bigD='Dollar'; smallD='Cent';}
else if(curr == 'IRD'){ bigD='Rupiah'; smallD='Sen';}
if((i==null)||(i < 0 )|| (i > 99999999999.99))
return 'Out of range';
Bn = math.floor(i/100000000); //Billion
i=i-Bn*100000000;
Gn = math.floor(i/1000000); //Million
i=i-Gn*1000000;
Kn = math.floor(i/1000); //Thousand
i=i-Kn*1000;
Dn = math.floor(i/100); //Hundred
if(Dn ==0)
tDn =math.floor(i/10);
else
tDn =math.floor(math.mod(integer.valueof(i),100)/10);
dec = math.mod(math.mod(integer.valueof(i*100),100), 100);
i=i-Dn*100;
n=math.mod(integer.valueof(i),10); //Ones
if(Bn !=0)
text = text + wtn(Bn, curr)+ ' Billion ';
if(Gn !=0)
text = text + wtn(Gn, curr)+ ' Million ';
if(Kn !=0)
text = text + wtn(Kn, curr)+ ' Thousand ';
if(Dn !=0)
text = text + wtn(Dn, curr)+ ' Hundred ';
if(tDn !=0 || n !=0){
if(text!='')
text = text + ' and ';
if(tDn <2){
text = text + ones[integer.valueof(tDn)*10 + n];
}else{
text = text + tens[integer.valueof(tDn)-1];
if(n!=0)
text = text + ' - ' + ones[n];
}
}
if(dec !=0){
text = text +' '+bigD+' and '+ wtn(dec, curr) + ' '+smallD;
}
if(text == '')
text = 'zero';
return text;
}
so if I call wtn(250.99,'USD');
I get "Two Hundred and Fifty Dollar and Ninety - Nine Cent"
As I said, the code is not perfect, if there are no decimal numbers, the code doesn't print "Dollar", some space issues etc.
The function can be added in a global class and called in before triggers to fill a text field with the amount in words, or it can be called in a visualforce print and so on.
Hope it helps someone.