RomanNumberFormatpublic class RomanNumberFormat extends Format Roman Number class. Not localized, since "Latin's a Dead Dead Language..."
and we don't display Roman Numbers differently in different Locales.
Filled with quick-n-dirty algorithms. |
Fields Summary |
---|
static char[] | A2RCharacters used in "Arabic to Roman", that is, format() methods. | protected int[] | stack | protected int | depth |
Methods Summary |
---|
public java.lang.String | format(double n)Format a given double as a Roman Numeral; just truncate to a
long, and call format(long).
return format((long)n);
| public java.lang.String | format(long n)Format a given long as a Roman Numeral. Just call the
three-argument form.
if (n <= 0 || n >= 4000)
throw new NumberFormatException(n + " must be > 0 && < 4000");
StringBuffer sb = new StringBuffer();
format(new Integer((int)n), sb, new FieldPosition(NumberFormat.INTEGER_FIELD));
return sb.toString();
| public java.lang.StringBuffer | format(java.lang.Object on, java.lang.StringBuffer sb, java.text.FieldPosition fp)
if (!(on instanceof Number))
throw new IllegalArgumentException(on + " must be a Number object");
if (fp.getField() != NumberFormat.INTEGER_FIELD)
throw new IllegalArgumentException(fp + " must be FieldPosition(NumberFormat.INTEGER_FIELD");
int n = ((Number)on).intValue(); // TODO: check in range.
// First, put the digits on a tiny stack. Must be 4 digits.
for (int i=0; i<4; i++) {
int d=n%10;
push(d);
// System.out.println("Pushed " + d);
n=n/10;
}
// Now pop and convert.
for (int i=0; i<4; i++) {
int ch = pop();
// System.out.println("Popped " + ch);
if (ch==0)
continue;
else if (ch <= 3) {
for(int k=1; k<=ch; k++)
sb.append(A2R[i][1]); // I
}
else if (ch == 4) {
sb.append(A2R[i][1]); // I
sb.append(A2R[i][2]); // V
}
else if (ch == 5) {
sb.append(A2R[i][2]); // V
}
else if (ch <= 8) {
sb.append(A2R[i][2]); // V
for (int k=6; k<=ch; k++)
sb.append(A2R[i][1]); // I
}
else { // 9
sb.append(A2R[i][1]);
sb.append(A2R[i][3]);
}
}
// fp.setBeginIndex(0);
// fp.setEndIndex(3);
return sb;
| public java.lang.Object | parseObject(java.lang.String what, java.text.ParsePosition where)Parse a generic object, returning an Object
throw new IllegalArgumentException("Parsing not implemented");
// TODO PARSING HERE
// return new Long(0);
| protected int | pop()
return stack[--depth];
| protected void | push(int n)
/* Implement a toy stack */
stack[depth++] = n;
|
|