uk.ydubey.formatter.numtoword.ThreeDigitNumberInWordsFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-realer Show documentation
Show all versions of virtdata-lib-realer Show documentation
With inspiration from other libraries
package uk.ydubey.formatter.numtoword;
import java.util.ArrayList;
import java.util.List;
public class ThreeDigitNumberInWordsFormatter extends TwoDigitNumberInWordsFormatter {
private static final ThreeDigitNumberInWordsFormatter INSTANCE = new ThreeDigitNumberInWordsFormatter();
protected ThreeDigitNumberInWordsFormatter() {
}
public static ThreeDigitNumberInWordsFormatter getInstance() {
return INSTANCE;
}
@Override
public int getLimit() {
return 999;
}
protected String parseAndFormat(int number) {
if(number <= super.getLimit()) {
return super.parseAndFormat(number);
} else {
List numberInWordsParts = new ArrayList();
final int hundreds = number / 100;
numberInWordsParts.add(super.parseAndFormat(hundreds));
numberInWordsParts.add("hundred");
String tensAndUnitsInWords = super.parseAndFormat(number % 100);
if(tensAndUnitsInWords.length() > 0) {
numberInWordsParts.add("and");
numberInWordsParts.add(tensAndUnitsInWords);
}
return join(numberInWordsParts);
}
}
}