Convert numbers from digital format to words.
This code provides a tool to convert a number from its digital value to the words format.
This release supports 3 built-in languages : English, French and Arabic.
Number x = 192345009;
NumberInWords numberInWords = new NumberInWords(x, Locale.ENGLISH);
System.out.println(numberInWords.toString());one hundred ninety two million, three hundred forty five thousand, nineThe decimals are supported too :
double value = 200.0098;
NumberInWords numberInWords = new NumberInWords(value);
System.out.println(numberInWords.toString());two hundred and zero zero ninety eightIf a customization is wanted, it will be by extending the Rules class and implement its method, the way the words format of a number is wanted.
Then, the new Extended Rules will be registered for a locale, if it depends on a Region or a Language :
LocalesRulesRegistry.register(Locale.XXXX, new MyNewRules());Or simply create a number by providing the new Rules :
NumberInWords numberInWords = new NumberInWords(29, new MyNewRules());Test are written in order to maintain the coverage of all the rules that implemented, but not to cover all the possible cases.
Coverage is ensured to be 100% on branches, lines, and methods.
- Some limitations are found, due to the big numbers, if a million multiple in a such rank does not exist, it will be replaced by
10^{rank}. - Decimal numbers beyond 10^15 are recommended to be dealt with as
Stringnot withdouble, because the decimal part is lost when converting it toBigDecimal
String x = "1000000000000000.001";
NumberInWords numberInWords = new NumberInWords(x);
String toLetters = numberInWords.toLetters();This will yield
one quadrillion and zero zero oneWhereas
double x = 1000000000000000.001;
NumberInWords numberInWords = new NumberInWords(x);
String toLetters = numberInWords.toLetters();Will yield
one quadrillionThe conversion can be used without needing to create an instance for each number, which is useful for the dependency injection oriented architectures :
Rules frenchRules = LocalesRulesRegistry.get(Locale.FRANCE);
INumberToWordsConverter numbersToWordsConverter = new NumberToWordsConverter();
String numberInWords = numbersToWordsConverter.convert("1500", frenchRules);
System.out.println(numberInWords);mille cinq cents