View Javadoc
1   package org.kuali.ole.sys.businessobject.format;
2   
3   import java.math.BigDecimal;
4   
5   import org.apache.log4j.Logger;
6   import org.kuali.rice.core.api.util.type.KualiDecimal;
7   import org.kuali.rice.core.web.format.BigDecimalFormatter;
8   
9   
10  /**
11   * This class is used to format explicit decimal value to BigDecimal objects.
12   */
13  public class ExplicitKualiDecimalFormatter extends BigDecimalFormatter {
14  	private static Logger LOG = Logger.getLogger(ExplicitKualiDecimalFormatter.class);
15  
16  	/**
17  	 * Converts the given String into a KualiDecimal with the final two characters being behind the decimal place
18  	 */
19  	@Override
20      protected Object convertToObject(String target) {
21  		BigDecimal value = (BigDecimal)super.convertToObject(addDecimalPoint(target));
22  		return new KualiDecimal(value);
23  	}
24  
25  	/**
26  	 * Adds the decimal point to the String
27  	 * @param amount the String representing the amount
28  	 * @return a new String, with a decimal inserted in the third to last place
29  	 */
30  	private String addDecimalPoint (String amount) {
31          if (!amount.contains(".")) {  //have to add decimal point if it's missing
32              int length = amount.length();
33              amount = amount.substring(0, length - 2) + "." + amount.substring(length - 2, length);
34          }
35          return amount;
36      }
37  }