All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.ydubey.formatter.numtoword.TwoDigitNumberInWordsFormatter Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
package uk.ydubey.formatter.numtoword;

import java.util.ArrayList;
import java.util.List;

public class TwoDigitNumberInWordsFormatter extends NumberFormatter {

    private static final String[] IN_WORDS = {
        "",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
    };

    private static final String[] TENS_IN_WORDS = {
        "",
        "ten",
        "twenty",
        "thirty",
        "forty",
        "fifty",
        "sixty",
        "seventy",
        "eighty",
        "ninety",
    };

    private static final TwoDigitNumberInWordsFormatter INSTANCE = new TwoDigitNumberInWordsFormatter();

    protected TwoDigitNumberInWordsFormatter() {
    }

    public static TwoDigitNumberInWordsFormatter getInstance() {
        return INSTANCE;
    }

    @Override
    public int getLimit() {
        return 99;
    }

    protected String parseAndFormat(int number) {
        List numberInWordsParts = new ArrayList();

        int unit = number % 10;
        int ten = (number % 100) / 10;

        if(number < IN_WORDS.length) {
            numberInWordsParts.add(IN_WORDS[number]);
        } else {
            numberInWordsParts.add(TENS_IN_WORDS[ten]);
            numberInWordsParts.add(IN_WORDS[unit]);
        }
        return join(numberInWordsParts);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy