001package org.kuali.ole.sys.businessobject.format;
002
003import java.math.BigDecimal;
004
005import org.apache.log4j.Logger;
006import org.kuali.rice.core.api.util.type.KualiDecimal;
007import org.kuali.rice.core.web.format.BigDecimalFormatter;
008
009
010/**
011 * This class is used to format explicit decimal value to BigDecimal objects.
012 */
013public class ExplicitKualiDecimalFormatter extends BigDecimalFormatter {
014        private static Logger LOG = Logger.getLogger(ExplicitKualiDecimalFormatter.class);
015
016        /**
017         * Converts the given String into a KualiDecimal with the final two characters being behind the decimal place
018         */
019        @Override
020    protected Object convertToObject(String target) {
021                BigDecimal value = (BigDecimal)super.convertToObject(addDecimalPoint(target));
022                return new KualiDecimal(value);
023        }
024
025        /**
026         * Adds the decimal point to the String
027         * @param amount the String representing the amount
028         * @return a new String, with a decimal inserted in the third to last place
029         */
030        private String addDecimalPoint (String amount) {
031        if (!amount.contains(".")) {  //have to add decimal point if it's missing
032            int length = amount.length();
033            amount = amount.substring(0, length - 2) + "." + amount.substring(length - 2, length);
034        }
035        return amount;
036    }
037}