1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ancientprogramming.fixedformat4j.format.impl;
17
18 import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
19
20 import java.text.DecimalFormat;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.commons.logging.Log;
25
26
27
28
29
30
31
32 public abstract class AbstractDecimalFormatter<T> extends AbstractNumberFormatter<T> {
33
34 private static final Log LOG = LogFactory.getLog(AbstractDecimalFormatter.class);
35
36 protected static final char DECIMAL_SEPARATOR;
37 protected static final char GROUPING_SEPARATOR;
38 protected static final String ZERO_STRING;
39 protected static final DecimalFormat FORMATTER;
40
41 static {
42 FORMATTER = new DecimalFormat();
43 FORMATTER.setDecimalSeparatorAlwaysShown(true);
44
45 DECIMAL_SEPARATOR = FORMATTER.getDecimalFormatSymbols().getDecimalSeparator();
46 GROUPING_SEPARATOR = FORMATTER.getDecimalFormatSymbols().getGroupingSeparator();
47 ZERO_STRING = "0" + DECIMAL_SEPARATOR + "0";
48
49 }
50
51 public String asString(T obj, FormatInstructions instructions) {
52
53 String rawString = obj != null ? FORMATTER.format(obj) : ZERO_STRING;
54 if (LOG.isDebugEnabled()) {
55 LOG.debug("rawString: " + rawString + " - G[" + GROUPING_SEPARATOR + "] D[" + DECIMAL_SEPARATOR + "]");
56 }
57 rawString = rawString.replaceAll("\\" + GROUPING_SEPARATOR, "");
58 boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter();
59
60 String beforeDelimiter = rawString.substring(0, rawString.indexOf(DECIMAL_SEPARATOR));
61 String afterDelimiter = rawString.substring(rawString.indexOf(DECIMAL_SEPARATOR)+1, rawString.length());
62 if (LOG.isDebugEnabled()) {
63 LOG.debug("beforeDelimiter[" + beforeDelimiter + "], afterDelimiter[" + afterDelimiter + "]");
64 }
65
66 int decimals = instructions.getFixedFormatDecimalData().getDecimals();
67
68 afterDelimiter = StringUtils.substring(afterDelimiter, 0, decimals);
69 afterDelimiter = StringUtils.rightPad(afterDelimiter, decimals, '0');
70
71 String delimiter = useDecimalDelimiter ? "" + instructions.getFixedFormatDecimalData().getDecimalDelimiter() : "";
72 String result = beforeDelimiter + delimiter + afterDelimiter;
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("result[" + result + "]");
75 }
76 return result;
77 }
78
79 protected String getStringToConvert(String string, FormatInstructions instructions) {
80 String toConvert;
81 boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter();
82 if(useDecimalDelimiter) {
83 char delimiter = instructions.getFixedFormatDecimalData().getDecimalDelimiter();
84 toConvert = string.replace(delimiter, '.');
85 } else {
86 int decimals = instructions.getFixedFormatDecimalData().getDecimals();
87 if (decimals > 0 && string.length() >= decimals) {
88 String beforeDelimiter = string.substring(0, string.length()-decimals);
89 String afterDelimiter = string.substring(string.length()-decimals, string.length());
90 toConvert = beforeDelimiter + '.' + afterDelimiter;
91 } else {
92 toConvert = string;
93 }
94
95 }
96 return toConvert;
97 }
98 }