Java Reference
In-Depth Information
* long, and call format(long).
*/
public
public
String
format
(
double
double
n
) {
return
return
format
((
long
long
)
n
);
}
/** Format a given long as a Roman Numeral. Just call the
* three-argument form.
*/
public
public
String
format
(
long
long
n
) {
iif
(
n
<=
0
||
n
>=
4000
)
throw
new
NumberFormatException
(
n
+
" must be > 0 && < 4000"
);
StringBuffer sb
=
new
throw new
new
StringBuffer
();
format
(
Integer
.
valueOf
((
int
int
)
n
),
sb
,
new
FieldPosition
(
NumberFormat
.
INTEGER_FIELD
));
return
new
return
sb
.
toString
();
}
/* Format the given Number as a Roman Numeral, returning the
* Stringbuffer (updated), and updating the FieldPosition.
* This method is the REAL FORMATTING ENGINE.
* Method signature is overkill, but required as a subclass of Format.
*/
public
public
StringBuffer
format
(
Object on
,
StringBuffer sb
,
FieldPosition fp
) {
iif
(!(
on
instanceof
instanceof
Number
))
new
IllegalArgumentException
(
on
+
" must be a Number object"
);
iif
(
fp
.
getField
() !=
NumberFormat
.
INTEGER_FIELD
)
throw
throw
throw new
new
IllegalArgumentException
(
fp
+
" must be FieldPosition(NumberFormat.INTEGER_FIELD"
);
throw new
int
int
n
= ((
Number
)
on
).
intValue
();
// TODO: check in range.
// First, put the digits on a tiny stack. Must be 4 digits.
for
for
(
int
int
i
=
0
;
i
<
4
;
i
++) {
int
int
d
=
n
%
10
;
push
(
d
);
// System.out.println("Pushed " + d);
n
=
n
/
10
;
}
// Now pop and convert.
for
for
(
int
int
i
=
0
;
i
<
4
;
i
++) {
int
int
ch
=
pop
();
// System.out.println("Popped " + ch);
iif
(
ch
==
0
)
continue
continue
;
else
else
if
(
ch
<=
3
) {