Converting Indian Rupees to words (with Paise) in Zoho Creator
Function 1
TwoDigitWords
string String.TwoDigitWords(int num)
{
double_words = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
teen_words = {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
single_words = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
//num = 99;
amount_in_words = "";
if(num > 0)
{
if(num < 10)
{
amount_in_words = single_words.get(num - 1);
}
else if(num < 20)
{
amount_in_words = teen_words.get(num - 10);
}
else if(num < 100)
{
first_digit_str = num.toString().substring(0,1);
first_digit = first_digit_str.toLong();
if(num % 10 == 0)
{
amount_in_words = double_words.get(first_digit - 2);
}
else
{
second_digit_str = num.toString().substring(1);
second_digit = second_digit_str.toLong();
amount_in_words = double_words.get(first_digit - 2) + " " + single_words.get(second_digit - 1);
}
}
}
return amount_in_words;
}
Function 2
ThreeDigitWords
string String.ThreeDigitWords(int num)
{
amount_in_words = "";
if(num > 99)
{
single_words = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
first_digit_str = num.toString().substring(0,1);
first_digit = first_digit_str.toLong();
amount_in_words = amount_in_words + single_words.get(first_digit - 1) + " hundred";
second_digit_str = num.toString().substring(1);
second_digit = second_digit_str.toLong();
if(second_digit > 0)
{
amount_in_words = amount_in_words + " and " + thisapp.String.TwoDigitWords(second_digit);
}
}
return amount_in_words;
}
Function 3
RupeeInWords (Recursive, to handle thousands of crores)
string String.RupeeInWords(int amount)
{
amount_in_words = "";
//Crores -> 9 or more Digits
if((amount / 10000000).toLong() > 0)
{
crore_num = (amount / 10000000).toLong();
crore_str = thisapp.String.Amount2Words(crore_num);
crore_num_len = crore_num.toString().length();
rest_amount = amount.toString().substring(crore_num_len).toLong();
rest_str = thisapp.String.Amount2Words(rest_amount);
amount_in_words = crore_str + " Crores " + rest_str;
}
//Lakhs -> 7 Digits
else if((amount / 100000).toLong() > 0)
{
lakh_word = thisapp.String.TwoDigitWords((amount / 100000).toLong()) + " lakhs";
rest_str = amount.toString().substring(2);
amount_in_words = lakh_word + " " + thisapp.String.Amount2Words(rest_str.toLong());
}
//Thousands -> 5 Digits
else if((amount / 1000).toLong() > 0)
{
thou_word = thisapp.String.TwoDigitWords((amount / 1000).toLong()) + " thousand";
rest_of_word = thisapp.String.ThreeDigitWords((amount % 1000).toLong());
amount_in_words = thou_word + " " + rest_of_word;
}
else if(amount.toLong() > 99)
{
amount_in_words = thisapp.String.ThreeDigitWords(amount.toLong());
}
else if(amount.toLong() > 0)
{
amount_in_words = thisapp.String.TwoDigitWords(amount.toLong());
}
return amount_in_words;
}
Function 4
Amount2Words (Actual function where the amount is passed)
string String.Amount2Words(float amount)
{
//amount.frac() not working for long numbers
paise_str = amount.toString().getSuffix(".");
if(paise_str != "")
{
paise_amount = paise_str.toLong();
paise_str = " and "+ thisapp.String.TwoDigitWords(paise_amount) + " paise";
}
rupee_str = String.RupeeInWords(amount.toLong());
return rupee_str + " rupees" + paise_str + " only";
}
Testing
Amount = 12341234567.89;
rupee_text = thisapp.String.Amount2Words(Amount);
//Output
//One thousand Two hundred and Thirty Four Crores Twelve lakhs Thirty Four thousand Five hundred and Sixty Seven rupees and Eighty Nine paise only
great and good job