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, nine
The decimals are supported too :
double value = 200.0098;
NumberInWords numberInWords = new NumberInWords(value);
System.out.println(numberInWords.toString());
two hundred and zero zero ninety eight
If 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.
10^{rank}
.String
not with double
, because the decimal part is lost when converting it to BigDecimal
String x = "1000000000000000.001";
NumberInWords numberInWords = new NumberInWords(x);
String toLetters = numberInWords.toLetters();
This will yield
one quadrillion and zero zero one
Whereas
double x = 1000000000000000.001;
NumberInWords numberInWords = new NumberInWords(x);
String toLetters = numberInWords.toLetters();
Will yield
one quadrillion
The 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