01: package com.technoetic.xplanner.format;
02:
03: import javax.servlet.http.HttpServletRequest;
04: import javax.servlet.jsp.PageContext;
05: import java.text.NumberFormat;
06: import java.text.ParseException;
07: import java.util.Locale;
08:
09: public class DecimalFormat extends AbstractFormat {
10: private java.text.DecimalFormat formatter = null;
11: private NumberFormat parser = null;
12:
13: public DecimalFormat(HttpServletRequest request) {
14: this (request.getLocale(), getFormat(request, "format.decimal"));
15: }
16:
17: public DecimalFormat(Locale locale, String format) {
18: formatter = (java.text.DecimalFormat) NumberFormat
19: .getNumberInstance(locale);
20: if (format != null) {
21: formatter.applyPattern(format);
22: }
23: parser = getParser(locale);
24: }
25:
26: private NumberFormat getParser(Locale locale) {
27: return java.text.DecimalFormat.getInstance(locale);
28: }
29:
30: public String format(double value) {
31: return formatter.format(value);
32: }
33:
34: public double parse(String value) throws ParseException {
35: value = value.trim();
36: return parser.parse(value).doubleValue();
37: }
38:
39: public static String format(PageContext pageContext, double value) {
40: return format((HttpServletRequest) pageContext.getRequest(),
41: value);
42: }
43:
44: public static String format(HttpServletRequest request, double value) {
45: return format(request, value, null);
46: }
47:
48: public static String format(PageContext pageContext, double value,
49: String zeroString) {
50: return (value == 0.0 && zeroString != null) ? zeroString
51: : new DecimalFormat((HttpServletRequest) pageContext
52: .getRequest()).format(value);
53: }
54:
55: public static String format(HttpServletRequest request,
56: double value, String zeroString) {
57: return (value == 0.0 && zeroString != null) ? zeroString
58: : new DecimalFormat(request).format(value);
59: }
60: }
|