FileDocCategorySizeDatePackage
RomanNumberFormat.javaAPI DocExample3160Fri Apr 06 22:35:54 BST 2001None

RomanNumberFormat

public 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.
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: RomanNumberFormat.java,v 1.5 2001/04/07 01:35:54 ian Exp $

Fields Summary
static char[]
A2R
Characters used in "Arabic to Roman", that is, format() methods.
protected int[]
stack
protected int
depth
Constructors Summary
Methods Summary
public java.lang.Stringformat(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.Stringformat(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.StringBufferformat(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();

		// 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.ObjectparseObject(java.lang.String what, java.text.ParsePosition where)
Parse a generic object, returning an Object

		throw new IllegalArgumentException("Parsing not implemented");
		// TODO PARSING HERE
		// if (!(what instanceof String)
		// 	throw new IllegalArgumentException(what + " must be String");
		// return new Long(0);
	
protected intpop()

		return stack[--depth];
	
protected voidpush(int n)


	/* Implement a toy stack */
	    
		stack[depth++] = n;