Giter VIP home page Giter VIP logo

alpha-number's Introduction

Write numbers in words

Maven Build Maven Test and Coverage

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.

Using as instance

Basic Example :

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

Decimal Format :

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

Customization

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());

Testing And Coverage

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.

Limitations

  • 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 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

Usage a stateless Service

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

References

FR : Noms_des_grands_nombres

EN : Names_of_large_numbers

EN : List_of_Illion_numbers

alpha-number's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

alpha-number's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.