01: /* $Id: BigDecimalFormat.java 626 2005-12-16 14:44:33Z hengels $ */
02: package org.conform.format;
03:
04: import java.text.*;
05: import java.math.BigDecimal;
06:
07: /**
08: * java.sql.Date
09: * @version $Revision: 626 $
10: */
11: public class BigDecimalFormat extends AbstractFormat implements Format {
12: int scale = -1;
13:
14: public BigDecimalFormat() {
15: message = "validation.unparsableNumber";
16: help = "+/-3456.78";
17: }
18:
19: public int getScale() {
20: return scale;
21: }
22:
23: public void setScale(int scale) {
24: this .scale = scale;
25: }
26:
27: public String getPattern() {
28: StringBuffer builder = new StringBuffer(scale + 1);
29: builder.append(".");
30: for (int i = 0; i < scale; i++)
31: builder.append('#');
32: return builder.toString();
33: }
34:
35: public void setPattern(String pattern) {
36: scale = 0;
37: for (int i = 0; i < pattern.length(); i++)
38: if ('#' == pattern.charAt(i))
39: scale++;
40: }
41:
42: public String format(Object value) {
43: value = scale((BigDecimal) value);
44: return value.toString();
45: }
46:
47: public Object parse(String string) throws ParseException {
48: try {
49: BigDecimal value = new BigDecimal(string);
50: value = scale((BigDecimal) value);
51: return value;
52: } catch (NumberFormatException e) {
53: throw new ParseException(string, 0);
54: }
55: }
56:
57: private BigDecimal scale(BigDecimal value) {
58: if (scale != -1)
59: value = value.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
60: return value;
61: }
62: }
|