Search in sources :

Example 16 with RuleBasedNumberFormat

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

the class RbnfTest method TestRuleSetDisplayName.

@Test
public void TestRuleSetDisplayName() {
    /**
     * Spellout rules for U.K. English.
     * I borrow the rule sets for TestRuleSetDisplayName()
     */
    final String ukEnglish = "%simplified:\n" + "    -x: minus >>;\n" + "    x.x: << point >>;\n" + "    zero; one; two; three; four; five; six; seven; eight; nine;\n" + "    ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n" + "        seventeen; eighteen; nineteen;\n" + "    20: twenty[->>];\n" + "    30: thirty[->>];\n" + "    40: forty[->>];\n" + "    50: fifty[->>];\n" + "    60: sixty[->>];\n" + "    70: seventy[->>];\n" + "    80: eighty[->>];\n" + "    90: ninety[->>];\n" + "    100: << hundred[ >>];\n" + "    1000: << thousand[ >>];\n" + "    1,000,000: << million[ >>];\n" + "    1,000,000,000,000: << billion[ >>];\n" + "    1,000,000,000,000,000: =#,##0=;\n" + "%alt-teens:\n" + "    =%simplified=;\n" + "    1000>: <%%alt-hundreds<[ >>];\n" + "    10,000: =%simplified=;\n" + "    1,000,000: << million[ >%simplified>];\n" + "    1,000,000,000,000: << billion[ >%simplified>];\n" + "    1,000,000,000,000,000: =#,##0=;\n" + "%%alt-hundreds:\n" + "    0: SHOULD NEVER GET HERE!;\n" + "    10: <%simplified< thousand;\n" + "    11: =%simplified= hundred>%%empty>;\n" + "%%empty:\n" + "    0:;" + "%ordinal:\n" + "    zeroth; first; second; third; fourth; fifth; sixth; seventh;\n" + "        eighth; ninth;\n" + "    tenth; eleventh; twelfth; thirteenth; fourteenth;\n" + "        fifteenth; sixteenth; seventeenth; eighteenth;\n" + "        nineteenth;\n" + "    twentieth; twenty->>;\n" + "    30: thirtieth; thirty->>;\n" + "    40: fortieth; forty->>;\n" + "    50: fiftieth; fifty->>;\n" + "    60: sixtieth; sixty->>;\n" + "    70: seventieth; seventy->>;\n" + "    80: eightieth; eighty->>;\n" + "    90: ninetieth; ninety->>;\n" + "    100: <%simplified< hundredth; <%simplified< hundred >>;\n" + "    1000: <%simplified< thousandth; <%simplified< thousand >>;\n" + "    1,000,000: <%simplified< millionth; <%simplified< million >>;\n" + "    1,000,000,000,000: <%simplified< billionth;\n" + "        <%simplified< billion >>;\n" + "    1,000,000,000,000,000: =#,##0=;" + "%default:\n" + "    -x: minus >>;\n" + "    x.x: << point >>;\n" + "    =%simplified=;\n" + "    100: << hundred[ >%%and>];\n" + "    1000: << thousand[ >%%and>];\n" + "    100,000>>: << thousand[>%%commas>];\n" + "    1,000,000: << million[>%%commas>];\n" + "    1,000,000,000,000: << billion[>%%commas>];\n" + "    1,000,000,000,000,000: =#,##0=;\n" + "%%and:\n" + "    and =%default=;\n" + "    100: =%default=;\n" + "%%commas:\n" + "    ' and =%default=;\n" + "    100: , =%default=;\n" + "    1000: , <%default< thousand, >%default>;\n" + "    1,000,000: , =%default=;" + "%%lenient-parse:\n" + "    & ' ' , ',' ;\n";
    ULocale.setDefault(ULocale.US);
    String[][] localizations = new String[][] { /* public rule sets*/
    { "%simplified", "%default", "%ordinal" }, /* display names in "en_US" locale*/
    { "en_US", "Simplified", "Default", "Ordinal" }, /* display names in "zh_Hans" locale*/
    { "zh_Hans", "\u7B80\u5316", "\u7F3A\u7701", "\u5E8F\u5217" }, /* display names in a fake locale*/
    { "foo_Bar_BAZ", "Simplified", "Default", "Ordinal" } };
    // Construct RuleBasedNumberFormat by rule sets and localizations list
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(ukEnglish, localizations, ULocale.US);
    RuleBasedNumberFormat f2 = new RuleBasedNumberFormat(ukEnglish, localizations);
    assertTrue("Check the two formatters' equality", formatter.equals(f2));
    // get displayName by name
    String[] ruleSetNames = formatter.getRuleSetNames();
    for (int i = 0; i < ruleSetNames.length; i++) {
        logln("Rule set name: " + ruleSetNames[i]);
        String RSName_defLoc = formatter.getRuleSetDisplayName(ruleSetNames[i]);
        assertEquals("Display name in default locale", localizations[1][i + 1], RSName_defLoc);
        String RSName_loc = formatter.getRuleSetDisplayName(ruleSetNames[i], ULocale.CHINA);
        assertEquals("Display name in Chinese", localizations[2][i + 1], RSName_loc);
    }
    // getDefaultRuleSetName
    String defaultRS = formatter.getDefaultRuleSetName();
    // you know that the default rule set is %simplified according to rule sets string ukEnglish
    assertEquals("getDefaultRuleSetName", "%simplified", defaultRS);
    // get locales of localizations
    ULocale[] locales = formatter.getRuleSetDisplayNameLocales();
    for (int i = 0; i < locales.length; i++) {
        logln(locales[i].getName());
    }
    // get displayNames
    String[] RSNames_defLoc = formatter.getRuleSetDisplayNames();
    for (int i = 0; i < RSNames_defLoc.length; i++) {
        assertEquals("getRuleSetDisplayNames in default locale", localizations[1][i + 1], RSNames_defLoc[i]);
    }
    String[] RSNames_loc = formatter.getRuleSetDisplayNames(ULocale.UK);
    for (int i = 0; i < RSNames_loc.length; i++) {
        assertEquals("getRuleSetDisplayNames in English", localizations[1][i + 1], RSNames_loc[i]);
    }
    RSNames_loc = formatter.getRuleSetDisplayNames(ULocale.CHINA);
    for (int i = 0; i < RSNames_loc.length; i++) {
        assertEquals("getRuleSetDisplayNames in Chinese", localizations[2][i + 1], RSNames_loc[i]);
    }
    RSNames_loc = formatter.getRuleSetDisplayNames(new ULocale("foo_Bar_BAZ"));
    for (int i = 0; i < RSNames_loc.length; i++) {
        assertEquals("getRuleSetDisplayNames in fake locale", localizations[3][i + 1], RSNames_loc[i]);
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) ULocale(android.icu.util.ULocale) Test(org.junit.Test)

Example 17 with RuleBasedNumberFormat

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

the class RbnfTest method TestFractionalRuleSet.

@Test
public void TestFractionalRuleSet() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(fracRules, Locale.ENGLISH);
    String[][] testData = { { "0", "0" }, { "1", "1" }, { "10", "10" }, { ".1", "1/10" }, { ".11", "1/9" }, { ".125", "1/8" }, { ".1428", "1/7" }, { ".1667", "1/6" }, { ".2", "1/5" }, { ".25", "1/4" }, { ".333", "1/3" }, { ".5", "1/2" }, { "1.1", "1 1/10" }, { "2.11", "2 1/9" }, { "3.125", "3 1/8" }, { "4.1428", "4 1/7" }, { "5.1667", "5 1/6" }, { "6.2", "6 1/5" }, { "7.25", "7 1/4" }, { "8.333", "8 1/3" }, { "9.5", "9 1/2" }, { ".2222", "2/9" }, { ".4444", "4/9" }, { ".5555", "5/9" }, { "1.2856", "1 2/7" } };
    // exact values aren't parsable from fractions
    doTest(formatter, testData, false);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 18 with RuleBasedNumberFormat

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

the class RbnfTest method TestMultiplePluralRules.

/**
 * Perform a simple spot check on the parsing going into an infinite loop for alternate rules.
 */
@Test
public void TestMultiplePluralRules() {
    // This is trying to model the feminine form, but don't worry about the details too much.
    // We're trying to test the plural rules where there are different prefixes.
    String ruRules = "%spellout-cardinal-feminine-genitive:" + "-x: минус >>;" + "x.x: << запятая >>;" + "0: ноля;" + "1: одной;" + "2: двух;" + "3: трех;" + "4: четырех;" + "5: пяти;" + "6: шести;" + "7: семи;" + "8: восьми;" + "9: девяти;" + "10: десяти;" + "11: одиннадцати;" + "12: двенадцати;" + "13: тринадцати;" + "14: четырнадцати;" + "15: пятнадцати;" + "16: шестнадцати;" + "17: семнадцати;" + "18: восемнадцати;" + "19: девятнадцати;" + "20: двадцати[ >>];" + "30: тридцати[ >>];" + "40: сорока[ >>];" + "50: пятидесяти[ >>];" + "60: шестидесяти[ >>];" + "70: семидесяти[ >>];" + "80: восемидесяти[ >>];" + "90: девяноста[ >>];" + "100: ста[ >>];" + "200: <<сот[ >>];" + "1000: << $(cardinal,one{тысяча}few{тысячи}other{тысяч})$[ >>];" + "1000000: =#,##0=;" + "%spellout-cardinal-feminine:" + "-x: минус >>;" + "x.x: << запятая >>;" + "0: ноль;" + "1: одна;" + "2: две;" + "3: три;" + "4: четыре;" + "5: пять;" + "6: шесть;" + "7: семь;" + "8: восемь;" + "9: девять;" + "10: десять;" + "11: одиннадцать;" + "12: двенадцать;" + "13: тринадцать;" + "14: четырнадцать;" + "15: пятнадцать;" + "16: шестнадцать;" + "17: семнадцать;" + "18: восемнадцать;" + "19: девятнадцать;" + "20: двадцать[ >>];" + "30: тридцать[ >>];" + "40: сорок[ >>];" + "50: пятьдесят[ >>];" + "60: шестьдесят[ >>];" + "70: семьдесят[ >>];" + "80: восемьдесят[ >>];" + "90: девяносто[ >>];" + "100: сто[ >>];" + "200: <<сти[ >>];" + "300: <<ста[ >>];" + "500: <<сот[ >>];" + "1000: << $(cardinal,one{тысяча}few{тысячи}other{тысяч})$[ >>];" + "1000000: =#,##0=;";
    RuleBasedNumberFormat ruFormatter = new RuleBasedNumberFormat(ruRules, new ULocale("ru"));
    try {
        Number result;
        if (1000 != (result = ruFormatter.parse(ruFormatter.format(1000))).doubleValue()) {
            errln("RuleBasedNumberFormat did not return the correct value. Got: " + result);
        }
        if (1000 != (result = ruFormatter.parse(ruFormatter.format(1000, "%spellout-cardinal-feminine-genitive"))).doubleValue()) {
            errln("RuleBasedNumberFormat did not return the correct value. Got: " + result);
        }
        if (1000 != (result = ruFormatter.parse(ruFormatter.format(1000, "%spellout-cardinal-feminine"))).doubleValue()) {
            errln("RuleBasedNumberFormat did not return the correct value. Got: " + result);
        }
    } catch (ParseException e) {
        errln(e.toString());
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) ULocale(android.icu.util.ULocale) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 19 with RuleBasedNumberFormat

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

the class RbnfTest method TestEnglishSpellout.

/**
 * Perform a simple spot check on the English spellout rules
 */
@Test
public void TestEnglishSpellout() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.SPELLOUT);
    String[][] testData = { { "1", "one" }, { "15", "fifteen" }, { "20", "twenty" }, { "23", "twenty-three" }, { "73", "seventy-three" }, { "88", "eighty-eight" }, { "100", "one hundred" }, { "106", "one hundred six" }, { "127", "one hundred twenty-seven" }, { "200", "two hundred" }, { "579", "five hundred seventy-nine" }, { "1,000", "one thousand" }, { "2,000", "two thousand" }, { "3,004", "three thousand four" }, { "4,567", "four thousand five hundred sixty-seven" }, { "15,943", "fifteen thousand nine hundred forty-three" }, { "2,345,678", "two million three hundred forty-five " + "thousand six hundred seventy-eight" }, { "-36", "minus thirty-six" }, { "234.567", "two hundred thirty-four point five six seven" } };
    doTest(formatter, testData, true);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 20 with RuleBasedNumberFormat

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

the class RbnfLenientScannerTest method TestGermanSpellout.

/**
 * Perform a simple spot check on the German spellout rules
 */
@Test
public void TestGermanSpellout() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.GERMANY, RuleBasedNumberFormat.SPELLOUT);
    formatter.setLenientScannerProvider(provider);
    formatter.setLenientParseMode(true);
    String[][] lpTestData = { { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" } };
    doLenientParseTest(formatter, lpTestData);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Aggregations

RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)55 Test (org.junit.Test)52 ULocale (android.icu.util.ULocale)14 Locale (java.util.Locale)14 ParseException (java.text.ParseException)8 NumberFormat (android.icu.text.NumberFormat)3 BigDecimal (android.icu.math.BigDecimal)2 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)2 DisplayContext (android.icu.text.DisplayContext)2 Random (java.util.Random)2 DecimalFormat (android.icu.text.DecimalFormat)1 BigInteger (java.math.BigInteger)1 FieldPosition (java.text.FieldPosition)1 NumberFormat (java.text.NumberFormat)1