Search in sources :

Example 41 with NumberFormat

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

the class NumberFormatRegressionTest method TestNBSPInPattern.

@Test
public void TestNBSPInPattern() {
    NumberFormat nf = null;
    String testcase;
    testcase = "ar_AE UNUM_CURRENCY";
    nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE"));
    checkNBSPPatternRT(testcase, nf);
    // if we don't have CLDR 1.6 data, bring out the problem anyways
    String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00";
    testcase = "ar_AE special pattern: " + SPECIAL_PATTERN;
    nf = new DecimalFormat();
    ((DecimalFormat) nf).applyPattern(SPECIAL_PATTERN);
    checkNBSPPatternRT(testcase, nf);
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 42 with NumberFormat

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

the class NumberFormatTest method TestAccountingCurrency.

@Test
public void TestAccountingCurrency() {
    String[][] tests = { // locale              num         curr fmt per loc     curr std fmt         curr acct fmt        rt
    { "en_US", "1234.5", "$1,234.50", "$1,234.50", "$1,234.50", "true" }, { "en_US@cf=account", "1234.5", "$1,234.50", "$1,234.50", "$1,234.50", "true" }, { "en_US", "-1234.5", "-$1,234.50", "-$1,234.50", "($1,234.50)", "true" }, { "en_US@cf=standard", "-1234.5", "-$1,234.50", "-$1,234.50", "($1,234.50)", "true" }, { "en_US@cf=account", "-1234.5", "($1,234.50)", "-$1,234.50", "($1,234.50)", "true" }, { "en_US", "0", "$0.00", "$0.00", "$0.00", "true" }, { "en_US", "-0.2", "-$0.20", "-$0.20", "($0.20)", "true" }, { "en_US@cf=standard", "-0.2", "-$0.20", "-$0.20", "($0.20)", "true" }, { "en_US@cf=account", "-0.2", "($0.20)", "-$0.20", "($0.20)", "true" }, { "ja_JP", "10000", "¥10,000", "¥10,000", "¥10,000", "true" }, { "ja_JP", "-1000.5", "-¥1,000", "-¥1,000", "(¥1,000)", "false" }, { "ja_JP@cf=account", "-1000.5", "(¥1,000)", "-¥1,000", "(¥1,000)", "false" }, { "de_DE", "-23456.7", "-23.456,70\u00A0€", "-23.456,70\u00A0€", "-23.456,70\u00A0€", "true" } };
    for (String[] data : tests) {
        ULocale loc = new ULocale(data[0]);
        double num = Double.parseDouble(data[1]);
        String fmtPerLocExpected = data[2];
        String fmtStandardExpected = data[3];
        String fmtAccountExpected = data[4];
        boolean rt = Boolean.parseBoolean(data[5]);
        NumberFormat fmtPerLoc = NumberFormat.getInstance(loc, NumberFormat.CURRENCYSTYLE);
        expect(fmtPerLoc, num, fmtPerLocExpected, rt);
        NumberFormat fmtStandard = NumberFormat.getInstance(loc, NumberFormat.STANDARDCURRENCYSTYLE);
        expect(fmtStandard, num, fmtStandardExpected, rt);
        NumberFormat fmtAccount = NumberFormat.getInstance(loc, NumberFormat.ACCOUNTINGCURRENCYSTYLE);
        expect(fmtAccount, num, fmtAccountExpected, rt);
    }
}
Also used : ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 43 with NumberFormat

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

the class NumberFormatTest method TestCurrencyFractionDigits.

/*
     * Testing currency driven max/min fraction digits problem
     * reported by ticket#7282
     */
@Test
public void TestCurrencyFractionDigits() {
    double value = 99.12345;
    // Create currency instance
    NumberFormat cfmt = NumberFormat.getCurrencyInstance(new ULocale("ja_JP"));
    String text1 = cfmt.format(value);
    // Reset the same currency and format the test value again
    cfmt.setCurrency(cfmt.getCurrency());
    String text2 = cfmt.format(value);
    // output1 and output2 must be identical
    if (!text1.equals(text2)) {
        errln("NumberFormat.format() should return the same result - text1=" + text1 + " text2=" + text2);
    }
}
Also used : ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 44 with NumberFormat

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

the class NumberFormatTest method TestCoverage.

@Test
public void TestCoverage() {
    // default locale
    NumberFormat fmt = NumberFormat.getNumberInstance();
    logln(fmt.format(new BigInteger("1234567890987654321234567890987654321", 10)));
    // default locale
    fmt = NumberFormat.getScientificInstance();
    logln(fmt.format(PI.INSTANCE));
    try {
        logln(fmt.format("12345"));
        errln("numberformat of string did not throw exception");
    } catch (Exception e) {
        logln("PASS: numberformat of string failed as expected");
    }
    int hash = fmt.hashCode();
    logln("hash code " + hash);
    logln("compare to string returns: " + fmt.equals(""));
    // For ICU 2.6 - alan
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat df = new DecimalFormat("'*&'' '\u00A4' ''&*' #,##0.00", US);
    df.setCurrency(Currency.getInstance("INR"));
    expect2(df, 1.0, "*&' \u20B9 '&* 1.00");
    expect2(df, -2.0, "-*&' \u20B9 '&* 2.00");
    df.applyPattern("#,##0.00 '*&'' '\u00A4' ''&*'");
    expect2(df, 2.0, "2.00 *&' \u20B9 '&*");
    expect2(df, -1.0, "-1.00 *&' \u20B9 '&*");
    java.math.BigDecimal r;
    r = df.getRoundingIncrement();
    if (r != null) {
        errln("FAIL: rounding = " + r + ", expect null");
    }
    if (df.isScientificNotation()) {
        errln("FAIL: isScientificNotation = true, expect false");
    }
    df.applyPattern("0.00000");
    df.setScientificNotation(true);
    if (!df.isScientificNotation()) {
        errln("FAIL: isScientificNotation = false, expect true");
    }
    df.setMinimumExponentDigits((byte) 2);
    if (df.getMinimumExponentDigits() != 2) {
        errln("FAIL: getMinimumExponentDigits = " + df.getMinimumExponentDigits() + ", expect 2");
    }
    df.setExponentSignAlwaysShown(true);
    if (!df.isExponentSignAlwaysShown()) {
        errln("FAIL: isExponentSignAlwaysShown = false, expect true");
    }
    df.setSecondaryGroupingSize(0);
    if (df.getSecondaryGroupingSize() != 0) {
        errln("FAIL: getSecondaryGroupingSize = " + df.getSecondaryGroupingSize() + ", expect 0");
    }
    expect2(df, 3.14159, "3.14159E+00");
    // DecimalFormatSymbols#getInstance
    DecimalFormatSymbols decsym1 = DecimalFormatSymbols.getInstance();
    DecimalFormatSymbols decsym2 = new DecimalFormatSymbols();
    if (!decsym1.equals(decsym2)) {
        errln("FAIL: DecimalFormatSymbols returned by getInstance()" + "does not match new DecimalFormatSymbols().");
    }
    decsym1 = DecimalFormatSymbols.getInstance(Locale.JAPAN);
    decsym2 = DecimalFormatSymbols.getInstance(ULocale.JAPAN);
    if (!decsym1.equals(decsym2)) {
        errln("FAIL: DecimalFormatSymbols returned by getInstance(Locale.JAPAN)" + "does not match the one returned by getInstance(ULocale.JAPAN).");
    }
    // DecimalFormatSymbols#getAvailableLocales/#getAvailableULocales
    Locale[] allLocales = DecimalFormatSymbols.getAvailableLocales();
    if (allLocales.length == 0) {
        errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableLocales");
    } else {
        logln("PASS: " + allLocales.length + " available locales returned by DecimalFormatSymbols.getAvailableLocales");
    }
    ULocale[] allULocales = DecimalFormatSymbols.getAvailableULocales();
    if (allULocales.length == 0) {
        errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableLocales");
    } else {
        logln("PASS: " + allULocales.length + " available locales returned by DecimalFormatSymbols.getAvailableULocales");
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigInteger(java.math.BigInteger) ParseException(java.text.ParseException) IOException(java.io.IOException) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 45 with NumberFormat

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

the class NumberFormatTest method TestCurrencyObject.

/**
 * Test the Currency object handling, new as of ICU 2.2.
 */
@Test
public void TestCurrencyObject() {
    NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.US);
    expectCurrency(fmt, null, 1234.56, "$1,234.56");
    expectCurrency(fmt, Currency.getInstance(Locale.FRANCE), 1234.56, // Euro
    "\u20AC1,234.56");
    expectCurrency(fmt, Currency.getInstance(Locale.JAPAN), 1234.56, // Yen
    "\u00A51,235");
    expectCurrency(fmt, Currency.getInstance(new Locale("fr", "CH", "")), 1234.56, // no more 0.05 rounding here, see cldrbug 5548
    "CHF1,234.56");
    expectCurrency(fmt, Currency.getInstance(Locale.US), 1234.56, "$1,234.56");
    fmt = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    expectCurrency(fmt, null, 1234.56, "1 234,56 \u20AC");
    expectCurrency(fmt, Currency.getInstance(Locale.JAPAN), 1234.56, // Yen
    "1 235 JPY");
    expectCurrency(fmt, Currency.getInstance(new Locale("fr", "CH", "")), 1234.56, // no more rounding here, see cldrbug 5548
    "1 234,56 CHF");
    expectCurrency(fmt, Currency.getInstance(Locale.US), 1234.56, "1 234,56 $US");
    expectCurrency(fmt, Currency.getInstance(Locale.FRANCE), 1234.56, // Euro
    "1 234,56 \u20AC");
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Aggregations

NumberFormat (android.icu.text.NumberFormat)105 Test (org.junit.Test)84 ULocale (android.icu.util.ULocale)39 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)35 DecimalFormat (android.icu.text.DecimalFormat)27 Locale (java.util.Locale)24 ParseException (java.text.ParseException)20 ParsePosition (java.text.ParsePosition)13 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)12 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)11 FieldPosition (java.text.FieldPosition)9 IOException (java.io.IOException)7 DateFormat (android.icu.text.DateFormat)5 Calendar (android.icu.util.Calendar)5 Date (java.util.Date)5 BigDecimal (android.icu.math.BigDecimal)4 DisplayContext (android.icu.text.DisplayContext)4 MeasureFormat (android.icu.text.MeasureFormat)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 PluralFormat (android.icu.text.PluralFormat)3