Search in sources :

Example 11 with RuleBasedNumberFormat

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

the class RbnfTest method TestChineseProcess.

/* Test the method
     *      public void process(StringBuffer buf, NFRuleSet ruleSet)
     */
@Test
public void TestChineseProcess() {
    String ruleWithChinese = "%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:;" + "%accounting:\n" + "    \u842c; \u842c; \u842c; \u842c; \u842c; \u842c; \u842c; \u842c;\n" + "        \u842c; \u842c;\n" + "    \u842c; \u842c; \u842c; \u842c; \u842c;\n" + "        \u842c; \u842c; \u842c; \u842c;\n" + "        \u842c;\n" + "    twentieth; \u96f6|>>;\n" + "    30: \u96f6; \u96f6|>>;\n" + "    40: \u96f6; \u96f6|>>;\n" + "    50: \u96f6; \u96f6|>>;\n" + "    60: \u96f6; \u96f6|>>;\n" + "    70: \u96f6; \u96f6|>>;\n" + "    80: \u96f6; \u96f6|>>;\n" + "    90: \u96f6; \u96f6|>>;\n" + "    100: <%simplified< \u96f6; <%simplified< \u96f6 >>;\n" + "    1000: <%simplified< \u96f6; <%simplified< \u96f6 >>;\n" + "    1,000,000: <%simplified< \u96f6; <%simplified< \u96f6 >>;\n" + "    1,000,000,000,000: <%simplified< \u96f6;\n" + "        <%simplified< \u96f6 >>;\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=;" + "%traditional:\n" + "    -x: \u3007| >>;\n" + "    x.x: << \u9ede >>;\n" + "    \u842c; \u842c; \u842c; \u842c; \u842c; \u842c; \u842c; \u842c; \u842c; \u842c;\n" + "    \u842c; \u842c; \u842c; \u842c; \u842c; \u842c; \u842c;\n" + "        \u842c; \u842c; \u842c;\n" + "    20: \u842c[->>];\n" + "    30: \u842c[->>];\n" + "    40: \u842c[->>];\n" + "    50: \u842c[->>];\n" + "    60: \u842c[->>];\n" + "    70: \u842c[->>];\n" + "    80: \u842c[->>];\n" + "    90: \u842c[->>];\n" + "    100: << \u842c[ >>];\n" + "    1000: << \u842c[ >>];\n" + "    1,000,000: << \u842c[ >>];\n" + "    1,000,000,000,000: << \u842c[ >>];\n" + "    1,000,000,000,000,000: =#,##0=;\n" + "%time:\n" + "    =0= sec.;\n" + "    60: =%%min-sec=;\n" + "    3600: =%%hr-min-sec=;\n" + "%%min-sec:\n" + "    0: *=00=;\n" + "    60/60: <0<>>;\n" + "%%hr-min-sec:\n" + "    0: *=00=;\n" + "    60/60: <00<>>;\n" + "    3600/60: <#,##0<:>>>;\n" + "%%post-process:android.icu.text.RBNFChinesePostProcessor\n";
    RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(ruleWithChinese, ULocale.CHINESE);
    String[] ruleNames = rbnf.getRuleSetNames();
    try {
        // Test with "null" rules
        rbnf.format(0.0, null);
        errln("This was suppose to return an exception for a null format");
    } catch (Exception e) {
    }
    for (int i = 0; i < ruleNames.length; i++) {
        try {
            rbnf.format(-123450.6789, ruleNames[i]);
        } catch (Exception e) {
            errln("RBNFChinesePostProcessor was not suppose to return an exception " + "when being formatted with parameters 0.0 and " + ruleNames[i]);
        }
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 12 with RuleBasedNumberFormat

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

the class RbnfTest method TestGermanSpellout.

/**
 * Perform a simple spot check on the German spellout rules
 */
@Test
public void TestGermanSpellout() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.GERMANY, RuleBasedNumberFormat.SPELLOUT);
    String[][] testData = { { "1", "eins" }, { "15", "f\u00fcnfzehn" }, { "20", "zwanzig" }, { "23", "drei\u00ADund\u00ADzwanzig" }, { "73", "drei\u00ADund\u00ADsiebzig" }, { "88", "acht\u00ADund\u00ADachtzig" }, { "100", "ein\u00ADhundert" }, { "106", "ein\u00ADhundert\u00ADsechs" }, { "127", "ein\u00ADhundert\u00ADsieben\u00ADund\u00ADzwanzig" }, { "200", "zwei\u00ADhundert" }, { "579", "f\u00fcnf\u00ADhundert\u00ADneun\u00ADund\u00ADsiebzig" }, { "1,000", "ein\u00ADtausend" }, { "2,000", "zwei\u00ADtausend" }, { "3,004", "drei\u00ADtausend\u00ADvier" }, { "4,567", "vier\u00ADtausend\u00ADf\u00fcnf\u00ADhundert\u00ADsieben\u00ADund\u00ADsechzig" }, { "15,943", "f\u00fcnfzehn\u00ADtausend\u00ADneun\u00ADhundert\u00ADdrei\u00ADund\u00ADvierzig" }, { "2,345,678", "zwei Millionen drei\u00ADhundert\u00ADf\u00fcnf\u00ADund\u00ADvierzig\u00ADtausend\u00AD" + "sechs\u00ADhundert\u00ADacht\u00ADund\u00ADsiebzig" } };
    doTest(formatter, testData, true);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 13 with RuleBasedNumberFormat

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

the class RbnfTest method TestInfinityNaN.

@Test
public void TestInfinityNaN() {
    String enRules = "%default:" + "-x: minus >>;" + "Inf: infinite;" + "NaN: not a number;" + "0: =#,##0=;";
    RuleBasedNumberFormat enFormatter = new RuleBasedNumberFormat(enRules, ULocale.ENGLISH);
    String[][] enTestData = { { "1", "1" }, { "\u221E", "infinite" }, { "-\u221E", "minus infinite" }, { "NaN", "not a number" } };
    doTest(enFormatter, enTestData, true);
    // Test the default behavior when the rules are undefined.
    enRules = "%default:" + "-x: ->>;" + "0: =#,##0=;";
    enFormatter = new RuleBasedNumberFormat(enRules, ULocale.ENGLISH);
    String[][] enDefaultTestData = { { "1", "1" }, { "\u221E", "∞" }, { "-\u221E", "-∞" }, { "NaN", "NaN" } };
    doTest(enFormatter, enDefaultTestData, true);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 14 with RuleBasedNumberFormat

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

the class RbnfTest method TestAllLocales.

@Test
public void TestAllLocales() {
    StringBuilder errors = new StringBuilder();
    String[] names = { " (spellout) ", " (ordinal) " // " (duration) " // English only
    };
    double[] numbers = { 45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111 };
    int count = numbers.length;
    Random r = (count <= numbers.length ? null : createRandom());
    for (ULocale loc : NumberFormat.getAvailableULocales()) {
        for (int j = 0; j < names.length; ++j) {
            RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(loc, j + 1);
            if (!loc.equals(fmt.getLocale(ULocale.ACTUAL_LOCALE))) {
                // Skip the redundancy
                break;
            }
            for (int c = 0; c < count; c++) {
                double n;
                if (c < numbers.length) {
                    n = numbers[c];
                } else {
                    n = (r.nextInt(10000) - 3000) / 16d;
                }
                String s = fmt.format(n);
                if (isVerbose()) {
                    logln(loc.getName() + names[j] + "success format: " + n + " -> " + s);
                }
                try {
                    // RBNF parse is extremely slow when lenient option is enabled.
                    // non-lenient parse
                    fmt.setLenientParseMode(false);
                    Number num = fmt.parse(s);
                    if (isVerbose()) {
                        logln(loc.getName() + names[j] + "success parse: " + s + " -> " + num);
                    }
                    if (j != 0) {
                        // TODO: Fix the ordinal rules.
                        continue;
                    }
                    if (n != num.doubleValue()) {
                        errors.append("\n" + loc + names[j] + "got " + num + " expected " + n);
                    }
                } catch (ParseException pe) {
                    String msg = loc.getName() + names[j] + "ERROR:" + pe.getMessage();
                    logln(msg);
                    errors.append("\n" + msg);
                }
            }
        }
    }
    if (errors.length() > 0) {
        errln(errors.toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Random(java.util.Random) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 15 with RuleBasedNumberFormat

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

the class RbnfTest method TestSwedishSpellout.

@Test
public void TestSwedishSpellout() {
    Locale locale = new Locale("sv", "", "");
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.SPELLOUT);
    String[][] testDataDefault = { { "101", "ett\u00ADhundra\u00ADett" }, { "123", "ett\u00ADhundra\u00ADtjugo\u00ADtre" }, { "1,001", "et\u00ADtusen ett" }, { "1,100", "et\u00ADtusen ett\u00ADhundra" }, { "1,101", "et\u00ADtusen ett\u00ADhundra\u00ADett" }, { "1,234", "et\u00ADtusen tv\u00e5\u00ADhundra\u00ADtrettio\u00ADfyra" }, { "10,001", "tio\u00ADtusen ett" }, { "11,000", "elva\u00ADtusen" }, { "12,000", "tolv\u00ADtusen" }, { "20,000", "tjugo\u00ADtusen" }, { "21,000", "tjugo\u00ADet\u00ADtusen" }, { "21,001", "tjugo\u00ADet\u00ADtusen ett" }, { "200,000", "tv\u00e5\u00ADhundra\u00ADtusen" }, { "201,000", "tv\u00e5\u00ADhundra\u00ADet\u00ADtusen" }, { "200,200", "tv\u00e5\u00ADhundra\u00ADtusen tv\u00e5\u00ADhundra" }, { "2,002,000", "tv\u00e5 miljoner tv\u00e5\u00ADtusen" }, { "12,345,678", "tolv miljoner tre\u00ADhundra\u00ADfyrtio\u00ADfem\u00ADtusen sex\u00ADhundra\u00ADsjuttio\u00AD\u00e5tta" }, { "123,456.789", "ett\u00ADhundra\u00ADtjugo\u00ADtre\u00ADtusen fyra\u00ADhundra\u00ADfemtio\u00ADsex komma sju \u00e5tta nio" }, { "-12,345.678", "minus tolv\u00ADtusen tre\u00ADhundra\u00ADfyrtio\u00ADfem komma sex sju \u00e5tta" } };
    logln("testing default rules");
    doTest(formatter, testDataDefault, true);
    String[][] testDataNeutrum = { { "101", "ett\u00adhundra\u00adett" }, { "1,001", "et\u00adtusen ett" }, { "1,101", "et\u00adtusen ett\u00adhundra\u00adett" }, { "10,001", "tio\u00adtusen ett" }, { "21,001", "tjugo\u00adet\u00adtusen ett" } };
    formatter.setDefaultRuleSet("%spellout-cardinal-neuter");
    logln("testing neutrum rules");
    doTest(formatter, testDataNeutrum, true);
    String[][] testDataYear = { { "101", "ett\u00adhundra\u00adett" }, { "900", "nio\u00adhundra" }, { "1,001", "et\u00adtusen ett" }, { "1,100", "elva\u00adhundra" }, { "1,101", "elva\u00adhundra\u00adett" }, { "1,234", "tolv\u00adhundra\u00adtrettio\u00adfyra" }, { "2,001", "tjugo\u00adhundra\u00adett" }, { "10,001", "tio\u00adtusen ett" } };
    formatter.setDefaultRuleSet("%spellout-numbering-year");
    logln("testing year rules");
    doTest(formatter, testDataYear, true);
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) 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