• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to convert edittext decimal number to word in android?

Hello Sir,
I have used bellow code for convert number to word. It is working nice. But when i enter input type numberDecimal in android EditText, Unfortunately stop my app. Please help me to convert numberDecimal to word....

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


public static String convertToWord(Integer i) {
//
if( i < 20) return units;
if( i < 100) return tens[i/10] + ((i % 10 > 0)? " " + convertToWord(i % 10):"");
if( i < 1000) return units[i/100] + " Hundred" + ((i % 100 > 0)?" and " + convertToWord(i % 100):"");
if( i < 1000000) return convertToWord(i / 1000) + " Thousand " + ((i % 1000 > 0)? " " + convertToWord(i % 1000):"") ;
return convertToWord(i / 1000000) + " Million " + ((i % 1000000 > 0)? " " + convertToWord(i % 1000000):"") ;
}
 
Do you mean if I type 10.3, the code should output "Ten point three"?
 
Working with the code you have, I would say the steps to do this are:-

1. Separate the decimal number into whole part, and fractional part
2. The whole part can be handled by your current code
3. For the fraction, iterate over each single digit, calling convertToWord() each time

This should build up the complete number.
 
Back
Top Bottom