1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by Enamul Haque, Apr 12, 2016.

  1. Enamul Haque

    Enamul Haque Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Mar 10, 2015

    Mar 10, 2015
    5
    0
    5
    Male
    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):"") ;
    }
     

    Advertisement

  2. Thom

    Thom  
    VIP Member
    Rank:
     #21
    Points:
    1,318
    Posts:
    12,006
    Joined:
    Feb 16, 2011

    Feb 16, 2011
    12,006
    4,973
    1,318
    Male
    Software Developer
    Keene, NH USA
    Welcome to Android Forums.

    This thread was moved to this forum that is dedicated to development subjects.

    ... Thom
     
  3. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,686
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,686
    11,132
    1,988
    Male
    Software developer
    South West of England
    Do you mean if I type 10.3, the code should output "Ten point three"?
     
  4. Enamul Haque

    Enamul Haque Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Mar 10, 2015

    Mar 10, 2015
    5
    0
    5
    Male
    Yes Sir,
    I want to 10.3(Out put will be ten point three)
    Please help me..
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,686
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,686
    11,132
    1,988
    Male
    Software developer
    South West of England
    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.
     
    El Presidente likes this.

Share This Page

Loading...