01: // Copyright (c) 2001 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.functions;
05:
06: import gnu.text.EnglishIntegerFormat;
07: import gnu.text.RomanIntegerFormat;
08: import gnu.math.*;
09:
10: public class IntegerFormat extends gnu.text.IntegerFormat {
11:
12: public IntegerFormat() {
13: }
14:
15: private static IntegerFormat plainDecimalFormat;
16:
17: public static IntegerFormat getInstance() {
18: if (plainDecimalFormat == null)
19: plainDecimalFormat = new IntegerFormat();
20: return plainDecimalFormat;
21: }
22:
23: public static java.text.Format getInstance(int base, int minWidth,
24: int padChar, int commaChar, int commaInterval, int flags) {
25: if (base == PARAM_UNSPECIFIED) {
26: if (padChar == PARAM_UNSPECIFIED
27: && padChar == PARAM_UNSPECIFIED
28: && commaChar == PARAM_UNSPECIFIED
29: && commaInterval == PARAM_UNSPECIFIED) {
30: // Common Lisp ~R format:
31: boolean seenColon = (flags & SHOW_GROUPS) != 0;
32: if ((flags & SHOW_PLUS) != 0)
33: return RomanIntegerFormat.getInstance(seenColon);
34: else
35: return EnglishIntegerFormat.getInstance(seenColon);
36: }
37: base = 10;
38: }
39: if (minWidth == PARAM_UNSPECIFIED)
40: minWidth = 1;
41: if (padChar == PARAM_UNSPECIFIED)
42: padChar = ' ';
43: if (commaChar == PARAM_UNSPECIFIED)
44: commaChar = ',';
45: if (commaInterval == PARAM_UNSPECIFIED)
46: commaInterval = 3;
47: if (base == 10 && minWidth == 1 && padChar == ' '
48: && commaChar == ',' && commaInterval == 3 && flags == 0)
49: return getInstance();
50: IntegerFormat fmt = new IntegerFormat();
51: fmt.base = base;
52: fmt.minWidth = minWidth;
53: fmt.padChar = padChar;
54: fmt.commaChar = commaChar;
55: fmt.commaInterval = commaInterval;
56: fmt.flags = flags;
57: return fmt;
58: }
59:
60: public String convertToIntegerString(Object arg, int radix) {
61: if (arg instanceof RealNum)
62: return ((RealNum) arg).toExactInt(Numeric.ROUND).toString(
63: radix);
64: else
65: return super.convertToIntegerString(arg, radix);
66: }
67: }
|