Search in sources :

Example 11 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class NumberFormatTest method TestCurrencyAmountCoverage.

@Test
public void TestCurrencyAmountCoverage() {
    CurrencyAmount ca, cb;
    try {
        ca = new CurrencyAmount(null, null);
        errln("NullPointerException should have been thrown.");
    } catch (NullPointerException ex) {
    }
    try {
        ca = new CurrencyAmount(new Integer(0), null);
        errln("NullPointerException should have been thrown.");
    } catch (NullPointerException ex) {
    }
    ca = new CurrencyAmount(new Integer(0), Currency.getInstance(new ULocale("ja_JP")));
    cb = new CurrencyAmount(new Integer(1), Currency.getInstance(new ULocale("ja_JP")));
    if (ca.equals(null)) {
        errln("Comparison should return false.");
    }
    if (!ca.equals(ca)) {
        errln("Comparision should return true.");
    }
    if (ca.equals(cb)) {
        errln("Comparison should return false.");
    }
}
Also used : BigInteger(java.math.BigInteger) ULocale(android.icu.util.ULocale) CurrencyAmount(android.icu.util.CurrencyAmount) Test(org.junit.Test)

Example 12 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class NumberFormatTest method TestFormat.

/*
     * Testing the method public StringBuffer format(Object number, ...)
     */
@Test
public void TestFormat() {
    NumberFormat nf = NumberFormat.getInstance();
    StringBuffer sb = new StringBuffer("dummy");
    FieldPosition fp = new FieldPosition(0);
    // Tests when "if (number instanceof Long)" is true
    try {
        nf.format(new Long("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a Long object. Error: " + e);
    }
    // Tests when "else if (number instanceof BigInteger)" is true
    try {
        nf.format((Object) new BigInteger("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a BigInteger object. Error: " + e);
    }
    // Tests when "else if (number instanceof java.math.BigDecimal)" is true
    try {
        nf.format((Object) new java.math.BigDecimal("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a java.math.BigDecimal object. Error: " + e);
    }
    // Tests when "else if (number instanceof android.icu.math.BigDecimal)" is true
    try {
        nf.format((Object) new android.icu.math.BigDecimal("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a android.icu.math.BigDecimal object. Error: " + e);
    }
    // Tests when "else if (number instanceof CurrencyAmount)" is true
    try {
        CurrencyAmount ca = new CurrencyAmount(0.0, Currency.getInstance(new ULocale("en_US")));
        nf.format((Object) ca, sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a CurrencyAmount object. Error: " + e);
    }
    // Tests when "else if (number instanceof Number)" is true
    try {
        nf.format(0.0, sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "to return an exception for a Number object. Error: " + e);
    }
    // Tests when "else" is true
    try {
        nf.format(new Object(), sb, fp);
        errln("NumberFormat.format(Object number, ...) was suppose to " + "return an exception for an invalid object.");
    } catch (Exception e) {
    }
    try {
        nf.format(new String("dummy"), sb, fp);
        errln("NumberFormat.format(Object number, ...) was suppose to " + "return an exception for an invalid object.");
    } catch (Exception e) {
    }
}
Also used : ULocale(android.icu.util.ULocale) FieldPosition(java.text.FieldPosition) ParseException(java.text.ParseException) IOException(java.io.IOException) BigDecimal(android.icu.math.BigDecimal) CurrencyAmount(android.icu.util.CurrencyAmount) BigInteger(java.math.BigInteger) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 13 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class NumberFormatTest method TestCurrencyFormatForMixParsing.

@Test
public void TestCurrencyFormatForMixParsing() {
    MeasureFormat curFmt = MeasureFormat.getCurrencyFormat(new ULocale("en_US"));
    String[] formats = { // string to be parsed
    "$1,234.56", "USD1,234.56", "US dollars1,234.56", "1,234.56 US dollars" };
    try {
        for (int i = 0; i < formats.length; ++i) {
            String stringToBeParsed = formats[i];
            CurrencyAmount parsedVal = (CurrencyAmount) curFmt.parseObject(stringToBeParsed);
            Number val = parsedVal.getNumber();
            if (!val.equals(new BigDecimal("1234.56"))) {
                errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the number. val=" + val);
            }
            if (!parsedVal.getCurrency().equals(Currency.getInstance("USD"))) {
                errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the currency");
            }
        }
    } catch (ParseException e) {
        errln("parse FAILED: " + e.toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) ParseException(java.text.ParseException) MeasureFormat(android.icu.text.MeasureFormat) CurrencyAmount(android.icu.util.CurrencyAmount) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 14 with CurrencyAmount

use of android.icu.util.CurrencyAmount 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

CurrencyAmount (android.icu.util.CurrencyAmount)14 Test (org.junit.Test)10 ULocale (android.icu.util.ULocale)8 ParseException (java.text.ParseException)5 BigDecimal (android.icu.math.BigDecimal)4 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)4 NumberFormat (android.icu.text.NumberFormat)4 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)4 MeasureFormat (android.icu.text.MeasureFormat)3 Currency (android.icu.util.Currency)3 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 ParsePosition (java.text.ParsePosition)2 Locale (java.util.Locale)2 StandardPlural (android.icu.impl.StandardPlural)1 ResourceReader (android.icu.impl.data.ResourceReader)1 TokenIterator (android.icu.impl.data.TokenIterator)1 DecimalFormat (android.icu.text.DecimalFormat)1 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)1 MeasureUnit (android.icu.util.MeasureUnit)1