ShowAll Questionssorted byDate Posted
bharatuuu.ax1725

# Currency to Words

Hi all,

I have a currecy field and needs to convert it into words.
Ex: Amount : \$ 1,10,521
It should be converted into text as "1 lakh 10 thousand five hundred and twenty 1 dollars only".

Plese suggest me

logontokartik
Please take a look at this blogpost. This should help.

http://salesforcewithkiran.blogspot.in/2013/05/number-to-words-in-apex.html
bharatuuu.ax1725
Hi,

I used that one but doesn't work for lakhs like for the example I have mentioned above.
logontokartik
OK. Try something lke this below. I modified the code a little from blog

```public with sharing class NumbersToWordsClass {
public string wordText{set;get;}

public string convert() {
wordText=convert(numberval);
return null;
}

public long numberval { get; set; }

public String[] units = new String[]{'Zero ','One ','Two ','Three ','Four ','Five ','Six ','Seven ','Eight ','Nine ','Ten ',
'Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ',
'Sixteen ','Seventeen ','Eighteen ','Nineteen '};
public String[] tens = new String[]{'','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'};

//This method is used to convert the integer to words
public  String convert(long i) {
//
if( i < 20)  return units[integer.valueOf(i)];
if( i < 100) return tens[integer.valueOf(i)/10] + ((math.mod(i , 10) > 0)? '' + convert(math.mod(i , 10)):'');
if( i < 1000) return units[integer.valueOf(i)/100] + ' Hundred ' + ((math.mod(i , 100) > 0)?' ' + convert(math.mod(i , 100)):'');
if( i < 10000) return units[integer.valueOf(i)/1000] + ' Thousand ' + ((math.mod(i , 1000) > 0)?' ' + convert(math.mod(i , 1000)):'');
if( i < 100000) return convert(i / 1000) + ' Thousand ' + ((math.mod(i , 1000) > 0)? '' + convert(math.mod(i ,1000)):'') ;
if( i < 1000000) return units[integer.valueOf(i)/100000] + ' Lakh ' + ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ;
if( i < 10000000) return convert(i / 100000) + ' Lakh ' + ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ;
if(i < 100000000) return units[integer.valueOf(i)/10000000] + ' Crore ' + ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ;
if(i < 1000000000) return convert(i / 10000000) + 'Crore ' + ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ;

return convert(i / 1000000000) + ' Crore ' + ((math.mod(i , 1000000000) > 0) ? '' + convert(math.mod(i , 1000000000)):'') ;
}
}```

To execute this do something like below, Do not include commas, use long formats only

```NumbersToWordsClass cls = new NumbersToWordsClass();
String wordText = cls.convert(110521);
system.debug(wordText);```

bharatuuu.ax1725
That worked thanks
İrem Güner
Hi Mohammed,

When I applied the apex class and the trigger you provided in the link above, it returns with the "word conversion of the integer portion" but does not return decimal places. For 121,56 It returns "One Hundred Twenty One and", does not bring "56"
Rajesh3699
Hi All,

I have given the answer here (Class AmountInWords_New) -> https://developer.salesforce.com/forums/?id=906F000000090L4IAI

Thank You,
Hosanagara.
Vishesh kant Aggarwal
try out this code it work perfectly .

public class testingClass2 {

public String[] units = new String[]{'Zero ','One ','Two ','Three ','Four ','Five ','Six ','Seven ','Eight ','Nine ','Ten ',
'Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ',
'Sixteen ','Seventeen ','Eighteen ','Nineteen '};

public String[] tens = new String[]{'','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'};

//This method is used to convert the integer to words
public String convert(long i) {

if( i < 20)  {
system.debug('in 20');
//return units[integer.valueOf(i)];
return units[i.intValue()];
}
if( i < 100) {
system.debug('in 100');
//return tens[i.intValue()/10] + ((math.mod(i , 10) > 0)? '' + convert(math.mod(i , 10)):'');
return tens[integer.valueOf(i)/10] + ((math.mod(i , 10) > 0)? '' + convert(math.mod(i , 10)):'');
}
if( i < 1000) {
system.debug('in 1000');
return units[integer.valueOf(i)/100] + ' Hundred ' + ((math.mod(i , 100) > 0)?' ' + convert(math.mod(i , 100)):'');
}
if( i < 10000) {
system.debug('in 10000');
return units[integer.valueOf(i)/1000] + ' Thousand ' + ((math.mod(i , 1000) > 0)?' ' + convert(math.mod(i , 1000)):'');
}
if( i < 100000) {
system.debug('in 100000');
return convert(i / 1000) + ' Thousand ' + ((math.mod(i , 1000) > 0)? '' + convert(math.mod(i ,1000)):'') ;
}
if( i < 1000000) {
system.debug('in 1000000');
return units[integer.valueOf(i)/100000] + ' Lakh ' + ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ;
}
if( i < 10000000) {
system.debug('in 10000000');
return convert(i / 100000) + ' Lakh ' + ((math.mod(i , 100000) > 0)? '' + convert(math.mod(i ,100000)):'') ;
}
if(i < 100000000) {
system.debug('in 100000000');
return units[integer.valueOf(i)/10000000] + ' Crore ' + ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ;
}
if(i < 1000000000) {
system.debug('in 1000000000');
return convert(i / 10000000) + 'Crore ' + ((math.mod(i , 10000000) > 0)? '' + convert(math.mod(i , 10000000)):'') ;
}

return convert(i / 1000000000) + ' Hundred Crore ' + ((math.mod(i , 1000000000) > 0) ? '' + convert(math.mod(i , 1000000000)):'') ;
}
}