Int to English words in Java

后端 未结 4 1263
情歌与酒
情歌与酒 2020-12-07 04:20

Here I am to ask something weird.

I would like to ask that is there any method/logic by which we can convert an integer value to a string value containing the Englis

4条回答
  •  青春惊慌失措
    2020-12-07 05:08

    While the accepted answer works, it would probably be best to use already-implemented functions to perform this. ICU4J contains a class com.ibm.icu.text.RuleBasedNumberFormat that can be used to perform this operation. It also supports languages other than English, and the reverse operation, parsing a text string to integer values.

    Here is an example, assuming that we have the ICU4J dependency in the classpath:

    import com.ibm.icu.text.RuleBasedNumberFormat;
    import java.util.Locale;
    
    RuleBasedNumberFormat nf = new RuleBasedNumberFormat (Locale.UK, RuleBasedNumberFormat.SPELLOUT);
    nf.format(24);
    // The result is "twenty-four"
    

提交回复
热议问题