AmountConverter is a generalized class which contains methods for getting the amount into words depending on the Indian or foreign Currency as well.
package fos; import fos.ReportsConstants; public class AmountConverter { private static AmountConverter instance = null; //public static String CURRENCY_INR = FOSConstants.LOCAL_CURR_CD; public static String CURRENCY_INR = "INR";//Indian rupees /** * Method getInstance. * @return AmountConverter */ public static AmountConverter getInstance(){ if(null == instance){ instance = new AmountConverter(); } return instance; } /** * Ref 001 * Method getAmountInWords returns the amount in word * for a number input in BSES format. * @param astrAmount * @return String */ public String getAmountInWords(String astrAmount, String strCurrCd){ StringBuffer sbfAmtInWords = new StringBuffer(); char ch; if (strCurrCd.equals(CURRENCY_INR)){ sbfAmtInWords.append(ReportsConstants.RUPEES); }else{ sbfAmtInWords.append(strCurrCd); } sbfAmtInWords.append(ReportsConstants.SPACE); for (int i = 0; i < astrAmount.length(); i++) { ch = astrAmount.charAt(i); switch (ch) { case '0': sbfAmtInWords.append(ReportsConstants.ZERO); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '1': sbfAmtInWords.append(ReportsConstants.ONE); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '2': sbfAmtInWords.append(ReportsConstants.TWO); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '3': sbfAmtInWords.append(ReportsConstants.THREE); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '4': sbfAmtInWords.append(ReportsConstants.FOUR); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '5': sbfAmtInWords.append(ReportsConstants.FIVE); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '6': sbfAmtInWords.append(ReportsConstants.SIX); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '7': sbfAmtInWords.append(ReportsConstants.SEVEN); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '8': sbfAmtInWords.append(ReportsConstants.EIGHT); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '9': sbfAmtInWords.append(ReportsConstants.NINE); sbfAmtInWords.append(ReportsConstants.SPACE); break; case '.': sbfAmtInWords.append(ReportsConstants.AND); sbfAmtInWords.append(ReportsConstants.SPACE); if (strCurrCd.equals(CURRENCY_INR)){ sbfAmtInWords.append(ReportsConstants.PAISE); }else{ sbfAmtInWords.append(ReportsConstants.COINS); } sbfAmtInWords.append(ReportsConstants.SPACE); break; default : break; } } sbfAmtInWords.append(ReportsConstants.ONLY); return sbfAmtInWords.toString(); } public String getAmountInWords(String astrAmount){ return getAmountInWords(astrAmount, CURRENCY_INR); } }