Search in sources :

Example 1 with CompactDecimalFormat

use of android.icu.text.CompactDecimalFormat in project j2objc by google.

the class CompactDecimalFormatTest method TestParsing.

@Test
public void TestParsing() {
    CompactDecimalFormat cdf = CompactDecimalFormat.getInstance(ULocale.ENGLISH, CompactStyle.LONG);
    try {
        cdf.parse("Parse for failure", new ParsePosition(0));
    } catch (UnsupportedOperationException e) {
        // Exception expected, thus return.
        return;
    }
    fail("Parsing currently unsupported, expected test to fail but passed");
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 2 with CompactDecimalFormat

use of android.icu.text.CompactDecimalFormat in project j2objc by google.

the class CompactDecimalFormatTest method TestBug12689.

@Test
public void TestBug12689() {
    if (logKnownIssue("12689", "CDF fails for numbers less than 1 thousand in most locales")) {
        return;
    }
    CompactDecimalFormat cdf;
    String result;
    cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("en"), CompactStyle.SHORT);
    result = cdf.format(new CurrencyAmount(123, Currency.getInstance("USD")));
    assertEquals("CDF should correctly format 123 with currency in 'en'", "$120", result);
    cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("it"), CompactStyle.SHORT);
    result = cdf.format(new CurrencyAmount(123, Currency.getInstance("EUR")));
    assertEquals("CDF should correctly format 123 with currency in 'it'", "120 €", result);
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) CurrencyAmount(android.icu.util.CurrencyAmount) Test(org.junit.Test)

Example 3 with CompactDecimalFormat

use of android.icu.text.CompactDecimalFormat in project j2objc by google.

the class CompactDecimalFormatTest method TestWriteAndReadObject.

@Test
public void TestWriteAndReadObject() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream objoutstream = new ObjectOutputStream(baos);
    CompactDecimalFormat cdf = CompactDecimalFormat.getInstance(ULocale.ENGLISH, CompactStyle.LONG);
    try {
        objoutstream.writeObject(cdf);
    } catch (NotSerializableException e) {
        if (logKnownIssue("10494", "PluralRules is not serializable")) {
            logln("NotSerializableException thrown when serializing CopactDecimalFormat");
        } else {
            errln("NotSerializableException thrown when serializing CopactDecimalFormat");
        }
    } finally {
        objoutstream.close();
    }
    // This test case relies on serialized byte stream which might be premature
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream objinstream = new ObjectInputStream(bais);
    try {
        cdf = (CompactDecimalFormat) objinstream.readObject();
    } catch (NotSerializableException e) {
        if (logKnownIssue("10494", "PluralRules is not de-serializable")) {
            logln("NotSerializableException thrown when deserializing CopactDecimalFormat");
        } else {
            errln("NotSerializableException thrown when deserializing CopactDecimalFormat");
        }
    } catch (ClassNotFoundException e) {
        errln("ClassNotFoundException: " + e.getMessage());
    } finally {
        objinstream.close();
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 4 with CompactDecimalFormat

use of android.icu.text.CompactDecimalFormat in project j2objc by google.

the class CompactDecimalFormatTest method getCDFInstance.

private static CompactDecimalFormat getCDFInstance(ULocale locale, CompactStyle style) {
    CompactDecimalFormat result = CompactDecimalFormat.getInstance(locale, style);
    // Our tests are written for two significant digits. We set explicitly here
    // because default significant digits may change.
    result.setMaximumSignificantDigits(2);
    return result;
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat)

Example 5 with CompactDecimalFormat

use of android.icu.text.CompactDecimalFormat in project j2objc by google.

the class CompactDecimalFormatTest method TestBig.

@Test
public void TestBig() {
    CompactDecimalFormat cdf = CompactDecimalFormat.getInstance(ULocale.ENGLISH, CompactStyle.LONG);
    BigInteger source_int = new BigInteger("31415926535897932384626433");
    assertEquals("BigInteger format wrong: ", "31,000,000,000,000 trillion", cdf.format(source_int));
    BigDecimal source_dec = new BigDecimal(source_int);
    assertEquals("BigDecimal format wrong: ", "31,000,000,000,000 trillion", cdf.format(source_dec));
}
Also used : BigInteger(java.math.BigInteger) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

CompactDecimalFormat (android.icu.text.CompactDecimalFormat)13 Test (org.junit.Test)10 CurrencyAmount (android.icu.util.CurrencyAmount)3 ULocale (android.icu.util.ULocale)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 FieldPosition (java.text.FieldPosition)1 ParsePosition (java.text.ParsePosition)1 LinkedHashSet (java.util.LinkedHashSet)1