Search in sources :

Example 86 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class DecimalFormatSymbolsTest method test_serialization.

// Test serialization mechanism of DecimalFormatSymbols
public void test_serialization() throws Exception {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRANCE);
    Currency currency = symbols.getCurrency();
    assertNotNull(currency);
    // serialize
    ByteArrayOutputStream byteOStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOStream = new ObjectOutputStream(byteOStream);
    objectOStream.writeObject(symbols);
    // and deserialize
    ObjectInputStream objectIStream = new ObjectInputStream(new ByteArrayInputStream(byteOStream.toByteArray()));
    DecimalFormatSymbols symbolsD = (DecimalFormatSymbols) objectIStream.readObject();
    // The associated currency will not persist
    currency = symbolsD.getCurrency();
    assertNotNull(currency);
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) ByteArrayInputStream(java.io.ByteArrayInputStream) Currency(java.util.Currency) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 87 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class DecimalFormatSymbolsTest method test_ConstructorLjava_util_Locale.

/**
     * @tests java.text.DecimalFormatSymbols#DecimalFormatSymbols(java.util.Locale)
     */
public void test_ConstructorLjava_util_Locale() {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(new Locale("en", "us"));
    assertEquals("Returned incorrect symbols", '%', dfs.getPercent());
}
Also used : Locale(java.util.Locale) DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Example 88 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class DecimalFormatTest method test_setNan_emptyString.

// https://code.google.com/p/android/issues/detail?id=59600
public void test_setNan_emptyString() throws Exception {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setNaN("");
    DecimalFormat df = new DecimalFormat();
    df.setDecimalFormatSymbols(dfs);
    df.format(Double.NaN);
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 89 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class DecimalFormatTest method test_format_roundingUnnecessaryArithmeticException.

public void test_format_roundingUnnecessaryArithmeticException() {
    final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    DecimalFormat decimalFormat = new DecimalFormat("00.0#E0", dfs);
    decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
    try {
        // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY,
        // throw ArithmeticException
        decimalFormat.format(99999, new StringBuffer(), new FieldPosition(0));
        fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
    // expected
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) FieldPosition(java.text.FieldPosition)

Example 90 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class DecimalFormatTest method test_parse_returnType.

// Test the type of the returned object
public void test_parse_returnType() {
    DecimalFormat form = (DecimalFormat) NumberFormat.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) NumberFormat.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 numbers will be parsed
    // into Long unless the value is out of the bound of Long or
    // some special values such as NaN or Infinity.
    form = (DecimalFormat) NumberFormat.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));
    // The last integers representable by long.
    number = form.parse("9223372036854775807.00", new ParsePosition(0));
    assertEquals(Long.class, number.getClass());
    number = form.parse("9223372036854775808.00", new ParsePosition(0));
    assertEquals(Double.class, number.getClass());
    // The first integers that need to be represented by double.
    number = form.parse("-9223372036854775808.00", new ParsePosition(0));
    assertEquals(Long.class, number.getClass());
    number = form.parse("-9223372036854775809.00", new ParsePosition(0));
    assertEquals(Double.class, number.getClass());
    // Even if parseIntegerOnly is set to true, NaN will be parsed to Double
    form = (DecimalFormat) NumberFormat.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) NumberFormat.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) NumberFormat.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("-92,233,720,368,547,758,080.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) NumberFormat.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) NumberFormat.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) NumberFormat.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) NumberFormat.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) NumberFormat.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) NumberFormat.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) NumberFormat.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) NumberFormat.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) NumberFormat.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("-92,233,720,368,547,758,080.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) NumberFormat.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("-92,233,720,368,547,758,080.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) {
        fail("Should not throw NPE");
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) ParsePosition(java.text.ParsePosition)

Aggregations

DecimalFormatSymbols (java.text.DecimalFormatSymbols)133 DecimalFormat (java.text.DecimalFormat)93 NumberFormat (java.text.NumberFormat)14 Locale (java.util.Locale)13 ParseException (java.text.ParseException)8 BigDecimal (java.math.BigDecimal)7 Currency (java.util.Currency)7 ObjectInputStream (java.io.ObjectInputStream)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 ParsePosition (java.text.ParsePosition)5 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)5 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)5 IndexInput (org.apache.lucene.store.IndexInput)5 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 BytesRef (org.apache.lucene.util.BytesRef)4 ArrayList (java.util.ArrayList)3