Search in sources :

Example 11 with CompactDecimalFormat

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

the class CompactDecimalFormatTest method TestDefaultSignificantDigits.

@Test
public void TestDefaultSignificantDigits() {
    // We are expecting two significant digits as default.
    CompactDecimalFormat cdf = CompactDecimalFormat.getInstance(ULocale.ENGLISH, CompactStyle.SHORT);
    assertEquals("Default significant digits", "12K", cdf.format(12345));
    assertEquals("Default significant digits", "1.2K", cdf.format(1234));
    assertEquals("Default significant digits", "120", cdf.format(123));
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) Test(org.junit.Test)

Example 12 with CompactDecimalFormat

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

the class CompactDecimalFormatTest method TestCharacterIterator.

@Test
public void TestCharacterIterator() {
    CompactDecimalFormat cdf = getCDFInstance(ULocale.forLanguageTag("sw"), CompactStyle.SHORT);
    AttributedCharacterIterator iter = cdf.formatToCharacterIterator(1234567);
    assertEquals("CharacterIterator", "M1.2", iterToString(iter));
    iter = cdf.formatToCharacterIterator(1234567);
    iter.setIndex(1);
    assertEquals("Attributes", NumberFormat.Field.INTEGER, iter.getAttribute(NumberFormat.Field.INTEGER));
    assertEquals("Attributes", 1, iter.getRunStart());
    assertEquals("Attributes", 2, iter.getRunLimit());
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) AttributedCharacterIterator(java.text.AttributedCharacterIterator) Test(org.junit.Test)

Example 13 with CompactDecimalFormat

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

the class CompactDecimalFormatTest method TestBug12422.

@Test
public void TestBug12422() {
    CompactDecimalFormat cdf;
    String result;
    // Bug #12422
    cdf = CompactDecimalFormat.getInstance(new ULocale("ar", "SA"), CompactDecimalFormat.CompactStyle.LONG);
    result = cdf.format(43000);
    assertEquals("CDF should correctly format 43000 in 'ar'", "٤٣ ألف", result);
    // Bug #12449
    cdf = CompactDecimalFormat.getInstance(new ULocale("ar"), CompactDecimalFormat.CompactStyle.SHORT);
    cdf.setMaximumSignificantDigits(3);
    result = cdf.format(1234);
    assertEquals("CDF should correctly format 1234 with 3 significant digits in 'ar'", "١٫٢٣ ألف", result);
    // Check currency formatting as well
    cdf = CompactDecimalFormat.getInstance(new ULocale("ar"), CompactDecimalFormat.CompactStyle.SHORT);
    result = cdf.format(new CurrencyAmount(43000f, Currency.getInstance("USD")));
    assertEquals("CDF should correctly format 43000 with currency in 'ar'", "US$ ٤٣ ألف", result);
    result = cdf.format(new CurrencyAmount(-43000f, Currency.getInstance("USD")));
    assertEquals("CDF should correctly format -43000 with currency in 'ar'", "US$ ؜-٤٣ ألف", result);
    // Extra locale with different positive/negative formats
    cdf = CompactDecimalFormat.getInstance(new ULocale("fi"), CompactDecimalFormat.CompactStyle.SHORT);
    result = cdf.format(new CurrencyAmount(43000f, Currency.getInstance("USD")));
    assertEquals("CDF should correctly format 43000 with currency in 'fi'", "43 t. $", result);
    result = cdf.format(new CurrencyAmount(-43000f, Currency.getInstance("USD")));
    assertEquals("CDF should correctly format -43000 with currency in 'fi'", "−43 t. $", result);
}
Also used : ULocale(android.icu.util.ULocale) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) CurrencyAmount(android.icu.util.CurrencyAmount) 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