# Number to word logic

My code to convert number to words is as below. Here i am getting output properly but if i have number 5,07,920 it is showing result five hundered and seven thousand nine hundred and twenty. i want five lakh seven thousand nine hundred and twenty.

``````public with sharing class PaySlipUtil{
static String[] to_19 = new string[]{ 'Zero','One','Two','Three','Four','Five','Six','Seven', 'Eight', 'Nine', 'Ten',  'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' };
static String[] tens = new string[]{ 'Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'};
static String[] denom = new string[]{ '','Thousand','Lakh','Million','Billion','Trillion','Quadrillion','Quintillion','S!xtillion','Septillion','Octillion','Nonillion','Decillion','Undecillion','Duodecillion','Tredecillion','Quattuordecillion','S!xdecillion','Septendecillion','Octodecillion','Novemdecillion','Vigintillion' };

// convert a value < 100 to English.
public static String convert_nn(integer val) {
if (val < 20)
if(val == 100)
return 'One Hundred';
for (integer v = 0; v < tens.size(); v++)
{
String dcap = tens[v];
integer dval = 20 + 10 * v;
if (dval + 10 > val)
{
if (Math.Mod(val,10) != 0)
return dcap + ' ' + to_19[Math.Mod(val,10)];
return dcap;
}
}
return 'Should never get here, less than 100 failure';
}

// convert a value < 1000 to english, special cased because it is the level that kicks
// off the < 100 special case. The rest are more general. This also allows you to
// get strings in the form of "forty-five hundred" if called directly.
public static String convert_nnn(integer val)
{
String word = '';
integer rem = val / 100;
integer mod = Math.mod(val,100);

if (rem > 0)
{

word = to_19[rem] + ' Hundred ';

if (mod > 0)
{
word += ' and ';
}
}
if (mod > 0)
word += convert_nn(mod);

return word;
}

public static String english_number(long val)
{
if (val < 100)
{
return convert_nn(val.intValue());
}
if (val < 1000)
{
return convert_nnn(val.intValue());
}

for (integer v = 0; v < denom.size(); v++)
{
integer didx = v - 1;
integer dval = (integer)Math.pow(1000, v);
if (dval > val)
{
integer mod = (integer)Math.pow(1000, didx);
integer l = (integer) val / mod;
integer r = (integer) val - (l * mod);

String ret = convert_nnn(l) + ' ' + denom[didx];
if (r > 0)
{
ret += ' '+english_number(r);
}
return ret;
}
}
return 'Should never get here, bottomed out in english_number';
}
}
``````
• For those wondering what a 'lakh' is, it is equivalent to 100,000 (one hundred thousand). It is a numerical unit common to India, but not widely used outside of the South Asia region. The decimal grouping placement of `5,07,920` is also not a mistake. What I think of as 'five hundred and seven thousand, nine hundred and twenty' is represented here as 'five lakh, seven thousand, nine hundred and twenty' (as indicated in the question) – Derek F Jan 30 '17 at 15:09

To change the algorithm, we'll need to add a new method, convert_nnnnnn, to cover the special situation that lakh introduces:

``````public static String convert_nnnnnn(Integer value) {
Integer lakh = value/100000,
thousands = Math.mod(value,100000)/1000,
hundreds = Math.mod(value,1000);
return (lakh > 0? ' ' + to_19[lakh] + ' Lakh':'')+
(thousands > 0? ' '+convert_nn(thousands) + ' ' + denom:'')+
(hundreds > 0? ' '+convert_nnn(hundreds):'');
}
``````

Then, we have to modify english_number to accommodate the special condition:

``````public static String english_number(long val) {
if (val < 100) {
return convert_nn(val.intValue());
}
if (val < 1000) {
return convert_nnn(val.intValue());
}
if (val < 1000000) {
return convert_nnnnnn(val.intValue());
}
for (integer v = 0; v < denom.size(); v++) {
integer didx = v - 1;
integer dval = (integer)Math.pow(1000, v);
if (dval > val) {
integer mod = (integer)Math.pow(1000, didx);
integer l = (integer) val / mod;
integer r = (integer) val - (l * mod);

String ret = convert_nnn(l) + ' ' + denom[didx];
if (r > 0) {
ret += ' '+english_number(r);
}
return ret;
}
}
return 'Should never get here, bottomed out in english_number';
}
``````

Note that since lakh is a special case, it should not be included in denom, because that array is only used for the general case of groups in sets of three (the algorithm does math to perform that calculation via `math.pow(1000, didx)`).

• And is good in British English english.stackexchange.com/questions/111765/… – Phil Hawthorn Jan 30 '17 at 11:45
• @PhilHawthorn I actually realized that the language bit was off topic either way, so I've simply removed the linguistic-oriented text. BrE is one correct dialect, but I suspect that the OP would be more interested in using Indian English anyways, and despite the number of Indian friends and associates I have, I honestly couldn't tell you if they use "and" or not that way. – sfdcfox Jan 30 '17 at 12:33