// Test the type of the returned object
public void test_parseLjava_lang_String_Ljava_text_ParsePosition() {
DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
Number number = form.parse("23.1", new ParsePosition(0));
assertTrue(number instanceof Double);
// Test parsed object of type double when
// parseBigDecimal is set to true
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
number = form.parse("23.1", new ParsePosition(0));
assertTrue(number instanceof Double);
form.setParseBigDecimal(true);
number = form.parse("23.1", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
assertEquals(new BigDecimal("23.1"), number);
// When parseIntegerOnly set to true, all float numbers will be parsed
// into Long.
// With the exception that, the value is out of the bound of Long or
// some special values such as NaN or Infinity.
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
form.setParseIntegerOnly(true);
number = form.parse("23.1f", new ParsePosition(0));
assertTrue(number instanceof Long);
number = form.parse("23.0", new ParsePosition(0));
assertTrue(number instanceof Long);
number = form.parse("-0.0", new ParsePosition(0));
assertTrue(number instanceof Long);
assertTrue(new Long(0).equals(number));
number = form.parse("-9,223,372,036,854,775,8080.00",
new ParsePosition(0));
assertTrue(number instanceof Double);
// Even if parseIntegerOnly is set to true, NaN will be parsed to Double
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
form.setParseIntegerOnly(true);
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
number = form.parse(symbols.getNaN(), new ParsePosition(0));
assertTrue(number instanceof Double);
// Even if parseIntegerOnly is set to true, Infinity will still be
// parsed to Double
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
form.setParseIntegerOnly(true);
symbols = new DecimalFormatSymbols();
number = form.parse(symbols.getInfinity(), new ParsePosition(0));
assertTrue(number instanceof Double);
// ParseBigDecimal take precedence of parseBigInteger
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
form.setParseIntegerOnly(true);
form.setParseBigDecimal(true);
number = form.parse("23.1f", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
number = form.parse("23.0", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
number = form.parse("-9,223,372,036,854,775,8080.00",
new ParsePosition(0));
assertFalse(number instanceof BigInteger);
assertTrue(number instanceof BigDecimal);
// Test whether the parsed object is of type float. (To be specific,
// they are of type Double)
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
number = form.parse("23.1f", new ParsePosition(0));
assertTrue(number instanceof Double);
form.setParseBigDecimal(true);
number = form.parse("23.1f", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
assertEquals(new BigDecimal("23.1"), number);
// Integer will be parsed to Long, unless parseBigDecimal is set to true
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
number = form.parse("123", new ParsePosition(0));
assertTrue(number instanceof Long);
form.setParseBigDecimal(true);
number = form.parse("123", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
assertEquals(new BigDecimal("123"), number);
// NaN will be parsed to Double, no matter parseBigDecimal set or not.
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
symbols = new DecimalFormatSymbols();
number = form.parse(symbols.getNaN() + "", new ParsePosition(0));
assertTrue(number instanceof Double);
form.setParseBigDecimal(true);
number = form.parse(symbols.getNaN() + "", new ParsePosition(0));
assertTrue(number instanceof Double);
// Infinity will be parsed to Double, no matter parseBigDecimal set or
// not.
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
symbols = new DecimalFormatSymbols();
number = form.parse(symbols.getInfinity(), new ParsePosition(0));
assertTrue(number instanceof Double);
assertEquals("Infinity", number.toString());
// When set bigDecimal to true, the result of parsing infinity
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
symbols = new DecimalFormatSymbols();
form.setParseBigDecimal(true);
number = form.parse(symbols.getInfinity(), new ParsePosition(0));
assertTrue(number instanceof Double);
assertEquals("Infinity", number.toString());
// Negative infinity will be parsed to double no matter parseBigDecimal
// set or not
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
symbols = new DecimalFormatSymbols();
number = form.parse("-" + symbols.getInfinity(), new ParsePosition(0));
assertTrue(number instanceof Double);
assertEquals("-Infinity", number.toString());
// When set bigDecimal to true, the result of parsing minus infinity
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
symbols = new DecimalFormatSymbols();
form.setParseBigDecimal(true);
number = form.parse("-" + symbols.getInfinity(), new ParsePosition(0));
assertTrue(number instanceof Double);
assertEquals("-Infinity", number.toString());
// -0.0 will be parsed to different type according to the combination of
// parseBigDecimal and parseIntegerOnly
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
// parseBigDecimal == true;
// parseIntegerOnly == false;
form.setParseBigDecimal(true);
number = form.parse("-0", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
number = form.parse("-0.0", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
// parseBigDecimal == false;
// parseIntegerOnly == true;
form.setParseBigDecimal(false);
form.setParseIntegerOnly(true);
number = form.parse("-0", new ParsePosition(0));
assertTrue(number instanceof Long);
number = form.parse("-0.0", new ParsePosition(0));
assertTrue(number instanceof Long);
// parseBigDecimal == false;
// parseIntegerOnly == false;
form.setParseBigDecimal(false);
form.setParseIntegerOnly(false);
number = form.parse("-0", new ParsePosition(0));
assertTrue(number instanceof Double);
number = form.parse("-0.0", new ParsePosition(0));
assertTrue(number instanceof Double);
// parseBigDecimal == true;
// parseIntegerOnly == true;
// parseBigDecimal take precedence of parseBigInteger
form.setParseBigDecimal(true);
form.setParseIntegerOnly(true);
number = form.parse("-0", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
number = form.parse("-0.0", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
number = form.parse("12.4", new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
// When parseBigDecimal is set to false, no matter how massive the
// mantissa part of a number is, the number will be parsed into Double
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
number = form.parse("9,223,372,036,854,775,808.00",
new ParsePosition(0));
assertTrue(number instanceof Double);
assertEquals("9.223372036854776E18", number.toString());
number = form.parse("-9,223,372,036,854,775,8080.00",
new ParsePosition(0));
assertTrue(number instanceof Double);
assertEquals("-9.223372036854776E19", number.toString());
// When parseBigDecimal is set to true, if mantissa part of number
// exceeds Long.MAX_VALUE, the number will be parsed into BigDecimal
form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
form.setParseBigDecimal(true);
number = form.parse("9,223,372,036,854,775,808.00",
new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
assertEquals(9.223372036854776E18, number.doubleValue(), 0);
number = form.parse("-9,223,372,036,854,775,8080.00",
new ParsePosition(0));
assertTrue(number instanceof BigDecimal);
assertEquals(-9.223372036854776E19, number.doubleValue(), 0);
// The minimum value of Long will be parsed to Long when parseBigDecimal
// is not set
ParsePosition pos = new ParsePosition(0);
DecimalFormat df = new DecimalFormat();
pos = new ParsePosition(0);
Number nb = df.parse("" + Long.MIN_VALUE, pos);
assertTrue(nb instanceof Long);
// The maximum value of Long will be parsed to Long when parseBigDecimal
// is set
pos = new ParsePosition(0);
df = new DecimalFormat();
pos = new ParsePosition(0);
nb = df.parse("" + Long.MAX_VALUE, pos);
assertTrue(nb instanceof Long);
// When parsing invalid string( which is neither consist of digits nor
// NaN/Infinity), a null will be returned.
pos = new ParsePosition(0);
df = new DecimalFormat();
try {
nb = df.parse("invalid", pos);
assertNull(nb);
} catch (NullPointerException e) {