DecimalFormat form = (DecimalFormat) nform;
// If Object(including null) is not of type Number,
// IllegalArgumentException will be thrown out
try {
form.format(new Object(), new StringBuffer(), new FieldPosition(0));
fail("Should throw IAE");
} catch (IllegalArgumentException e) {
// expected
}
try {
form.format(null, new StringBuffer(), new FieldPosition(0));
fail("Should throw IAE");
} catch (IllegalArgumentException e) {
// expected
}
// When StringBuffer == null || FieldPosition == null
// NullPointerException will be thrown out.
try {
form.format(new Double(1.9), null, new FieldPosition(0));
fail("Should throw NPE");
} catch (NullPointerException e) {
// expected
}
try {
form.format(new Double(1.3), new StringBuffer(), null);
fail("Should throw NPE");
} catch (NullPointerException e) {
// expected
}
try {
form.format(new Double(1.4), null, null);
fail("Should throw NPE");
} catch (NullPointerException e) {
// expected
}
try {
form.format(new Object(), null, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
FieldPosition pos;
StringBuffer out;
DecimalFormat format = (DecimalFormat) NumberFormat
.getInstance(Locale.US);
// format maxLong
pos = new FieldPosition(0);
out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L1: " + out, out.toString().equals(
"9,223,372,036,854,775,807"));
// format minLong
pos = new FieldPosition(0);
out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L2: " + out, out.toString().equals(
"-9,223,372,036,854,775,808"));
// format maxLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String
.valueOf(Long.MAX_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI1: " + out, out.toString().equals(
"9,223,372,036,854,775,807"));
// format minLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String
.valueOf(Long.MIN_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI2: " + out, out.toString().equals(
"-9,223,372,036,854,775,808"));
// format maxLong + 1
java.math.BigInteger big;
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE))
.add(new java.math.BigInteger("1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI3: " + out, out.toString().equals(
"9,223,372,036,854,775,808"));
// format minLong - 1
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE))
.add(new java.math.BigInteger("-1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI4: " + out, out.toString().equals(
"-9,223,372,036,854,775,809"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51.348"),
new StringBuffer(), pos);
assertTrue("Wrong result BD1: " + out, out.toString().equals("51.348"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51"), new StringBuffer(),
pos);
assertTrue("Wrong result BD2: " + out, out.toString().equals("51"));
// format big decimal Double.MAX_VALUE * 2
java.math.BigDecimal bigDecimal;
pos = new FieldPosition(0);
final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408,"
+ "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077,"
+ "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653,"
+ "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152,"
+ "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806,"
+ "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476,"
+ "354,361,838,599,762,500,808,052,368,249,716,736";
bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(
Double.MAX_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
assertTrue("Wrong result BDmax2: " + out, out.toString().equals(
doubleMax2));
// format big decimal Double.MIN_VALUE + Double.MIN_VALUE
// and Double.MIN_VALUE - Double.MIN_VALUE
pos = new FieldPosition(0);
bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal(
Double.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);