Number to Word Apex Code
Public Class NumberToWordClass
{
public String ru{get;set;}
public Void NumberToWord(Integer no)
{
ru='';
String[] word = new List<String>
{
'',' ONE ',' TWO ',' THREE ',' FOUR ',' FIVE ',' SIX ',' SEVEN ',' EIGHT ',' NINE ',' TEN ',
' ELEVEN ',' TWELVE ',' THIRTEEN ',' FOURTEEN ',' FIFTEEN ',' SIXTEEN ',' SEVENTEEN ',' EIGHTEEN ',' NINETEEN '
};
String[] tens = new List<String>
{
'','','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY'
};
Integer n1,n2;
n1=math.mod(no,100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1);
}
else if(n1>19)
{
ru=tens.get(n1/10)+word.get(math.mod(n1,10));
system.debug('ru***'+ru);
}
}
n1=math.mod((no/100),10);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' HUNDRED AND '+ru;
system.debug('ru***'+ru);
}
}
n1=math.mod((no/1000),100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' THOUSAND '+ru;
system.debug('ru***'+ru);
}
else if(n1>19)
{
ru=tens.get(n1/10)+' '+word.get(math.mod(n1,10))+' THOUSAND '+ru;
system.debug('ru***'+ru);
}
}
n1=math.mod((no/100000),100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' LAKH '+ru;
system.debug('ru***'+ru);
}
else if(n1>19)
{
ru=tens.get(n1/10)+' '+word.get(math.mod(n1,10))+' LAKH '+ru;
system.debug('ru***'+ru);
}
}
n1=math.mod((no/10000000),100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' CRORE '+ru;
system.debug('ru***'+ru);
}
else if(n1>19)
{
ru=tens.get(n1/10)+' '+word.get(math.mod(n1,10))+' CRORE '+ru;
system.debug('ru***'+ru);
}
}
if(ru.endswith('AND '))
{
ru = ru.substring(0,ru.length()-4);
system.debug('ru***'+ru);
}
}
}
Open the Developer Console and Test
NumberToWordClass obj = new NumberToWordClass();
obj.NumberToWord(125520);
OutPut: ONE LAKH TWENTY FIVE THOUSAND FIVE HUNDRED AND TWENTY
{
public String ru{get;set;}
public Void NumberToWord(Integer no)
{
ru='';
String[] word = new List<String>
{
'',' ONE ',' TWO ',' THREE ',' FOUR ',' FIVE ',' SIX ',' SEVEN ',' EIGHT ',' NINE ',' TEN ',
' ELEVEN ',' TWELVE ',' THIRTEEN ',' FOURTEEN ',' FIFTEEN ',' SIXTEEN ',' SEVENTEEN ',' EIGHTEEN ',' NINETEEN '
};
String[] tens = new List<String>
{
'','','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY'
};
Integer n1,n2;
n1=math.mod(no,100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1);
}
else if(n1>19)
{
ru=tens.get(n1/10)+word.get(math.mod(n1,10));
system.debug('ru***'+ru);
}
}
n1=math.mod((no/100),10);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' HUNDRED AND '+ru;
system.debug('ru***'+ru);
}
}
n1=math.mod((no/1000),100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' THOUSAND '+ru;
system.debug('ru***'+ru);
}
else if(n1>19)
{
ru=tens.get(n1/10)+' '+word.get(math.mod(n1,10))+' THOUSAND '+ru;
system.debug('ru***'+ru);
}
}
n1=math.mod((no/100000),100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' LAKH '+ru;
system.debug('ru***'+ru);
}
else if(n1>19)
{
ru=tens.get(n1/10)+' '+word.get(math.mod(n1,10))+' LAKH '+ru;
system.debug('ru***'+ru);
}
}
n1=math.mod((no/10000000),100);
if(n1>0)
{
if(n1<=19)
{
ru=word.get(n1)+' CRORE '+ru;
system.debug('ru***'+ru);
}
else if(n1>19)
{
ru=tens.get(n1/10)+' '+word.get(math.mod(n1,10))+' CRORE '+ru;
system.debug('ru***'+ru);
}
}
if(ru.endswith('AND '))
{
ru = ru.substring(0,ru.length()-4);
system.debug('ru***'+ru);
}
}
}
Open the Developer Console and Test
NumberToWordClass obj = new NumberToWordClass();
obj.NumberToWord(125520);
OutPut: ONE LAKH TWENTY FIVE THOUSAND FIVE HUNDRED AND TWENTY
Comments
Post a Comment