Kirill Kirill - 4 years ago 205
Java Question

Convert double value user input into words in Java

Is there a way to convert double value into words using not much code?

Read several questions about it here,but the solutions there seem to be too long and inefficient

Answer Source

You may try ICU4J. ICU4J is a library which provides Unicode and Globalization support.

ICU provides number spellout rules for several locales. Below is an example for english:

import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.text.RuleBasedNumberFormat;
import com.ibm.icu.util.ULocale;

public class NewClass4 {       
   public static void main(String[] args){
      ULocale locale = new ULocale("En");
      String userInput = "123.45";
      Double d = Double.parseDouble(userInput);
      NumberFormat formatter = new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.SPELLOUT);
      String result = formatter.format(d);
      System.out.println(result);
   }
}

Output : one hundred twenty-three point four five

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download