Search in sources :

Example 21 with RuleBasedNumberFormat

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

the class RbnfTest method TestRounding.

@Test
public void TestRounding() {
    RuleBasedNumberFormat enFormatter = new RuleBasedNumberFormat(ULocale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
    String[][] enTestFullData = { { "0", "zero" }, { "0.4", "zero point four" }, { "0.49", "zero point four nine" }, { "0.5", "zero point five" }, { "0.51", "zero point five one" }, { "0.99", "zero point nine nine" }, { "1", "one" }, { "1.01", "one point zero one" }, { "1.49", "one point four nine" }, { "1.5", "one point five" }, { "1.51", "one point five one" }, // 2^52 / 10
    { "450359962737049.6", "four hundred fifty trillion three hundred fifty-nine billion nine hundred sixty-two million seven hundred thirty-seven thousand forty-nine point six" }, // 2^52 + 1 / 10
    { "450359962737049.7", "four hundred fifty trillion three hundred fifty-nine billion nine hundred sixty-two million seven hundred thirty-seven thousand forty-nine point seven" } };
    doTest(enFormatter, enTestFullData, false);
    enFormatter.setMaximumFractionDigits(0);
    enFormatter.setRoundingMode(BigDecimal.ROUND_HALF_EVEN);
    String[][] enTestIntegerData = { { "0", "zero" }, { "0.4", "zero" }, { "0.49", "zero" }, { "0.5", "zero" }, { "0.51", "one" }, { "0.99", "one" }, { "1", "one" }, { "1.01", "one" }, { "1.49", "one" }, { "1.5", "two" }, { "1.51", "two" } };
    doTest(enFormatter, enTestIntegerData, false);
    enFormatter.setMaximumFractionDigits(1);
    enFormatter.setRoundingMode(BigDecimal.ROUND_HALF_EVEN);
    String[][] enTestTwoDigitsData = { { "0", "zero" }, { "0.04", "zero" }, { "0.049", "zero" }, { "0.05", "zero" }, { "0.051", "zero point one" }, { "0.099", "zero point one" }, { "10.11", "ten point one" }, { "10.149", "ten point one" }, { "10.15", "ten point two" }, { "10.151", "ten point two" } };
    doTest(enFormatter, enTestTwoDigitsData, false);
    enFormatter.setMaximumFractionDigits(3);
    enFormatter.setRoundingMode(BigDecimal.ROUND_DOWN);
    String[][] enTestThreeDigitsDownData = { // Not 4.299!
    { "4.3", "four point three" } };
    doTest(enFormatter, enTestThreeDigitsDownData, false);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 22 with RuleBasedNumberFormat

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

the class RbnfTest method TestCoverage.

@Test
public void TestCoverage() {
    String durationInSecondsRules = // main rule set for formatting with words
    "%with-words:\n" + // take care of singular and plural forms of "second"
    "    0 seconds; 1 second; =0= seconds;\n" + // use %%min to format values greater than 60 seconds
    "    60/60: <%%min<[, >>];\n" + // when when there are zero minutes)
    "    3600/60: <%%hr<[, >>>];\n" + // of "minute"
    "%%min:\n" + "    0 minutes; 1 minute; =0= minutes;\n" + // of "hour"
    "%%hr:\n" + "    0 hours; 1 hour; =0= hours;\n" + // main rule set for formatting in numerals
    "%in-numerals:\n" + // values below 60 seconds are shown with "sec."
    "    =0= sec.;\n" + // values below 3,600 seconds...
    "    60: =%%min-sec=;\n" + // and above
    "    3600: =%%hr-min-sec=;\n" + // a leading zero
    "%%min-sec:\n" + "    0: :=00=;\n" + "    60/60: <0<>>;\n" + // shown, and always shown with two digits
    "%%hr-min-sec:\n" + "    0: :=00=;\n" + "    60/60: <00<>>;\n" + "    3600/60: <#,##0<:>>>;\n" + // as delimiters between hours, minutes, and seconds
    "%%lenient-parse:\n" + "    & : = . = ' ' = -;\n";
    // extra calls to boost coverage numbers
    RuleBasedNumberFormat fmt0 = new RuleBasedNumberFormat(RuleBasedNumberFormat.SPELLOUT);
    RuleBasedNumberFormat fmt1 = (RuleBasedNumberFormat) fmt0.clone();
    RuleBasedNumberFormat fmt2 = new RuleBasedNumberFormat(RuleBasedNumberFormat.SPELLOUT);
    if (!fmt0.equals(fmt0)) {
        errln("self equality fails");
    }
    if (!fmt0.equals(fmt1)) {
        errln("clone equality fails");
    }
    if (!fmt0.equals(fmt2)) {
        errln("duplicate equality fails");
    }
    String str = fmt0.toString();
    logln(str);
    RuleBasedNumberFormat fmt3 = new RuleBasedNumberFormat(durationInSecondsRules);
    if (fmt0.equals(fmt3)) {
        errln("nonequal fails");
    }
    if (!fmt3.equals(fmt3)) {
        errln("self equal 2 fails");
    }
    str = fmt3.toString();
    logln(str);
    String[] names = fmt3.getRuleSetNames();
    try {
        fmt3.setDefaultRuleSet(null);
        fmt3.setDefaultRuleSet("%%foo");
        errln("sdrf %%foo didn't fail");
    } catch (Exception e) {
        logln("Got the expected exception");
    }
    try {
        fmt3.setDefaultRuleSet("%bogus");
        errln("sdrf %bogus didn't fail");
    } catch (Exception e) {
        logln("Got the expected exception");
    }
    try {
        str = fmt3.format(2.3, names[0]);
        logln(str);
        str = fmt3.format(2.3, "%%foo");
        errln("format double %%foo didn't fail");
    } catch (Exception e) {
        logln("Got the expected exception");
    }
    try {
        str = fmt3.format(123L, names[0]);
        logln(str);
        str = fmt3.format(123L, "%%foo");
        errln("format double %%foo didn't fail");
    } catch (Exception e) {
        logln("Got the expected exception");
    }
    RuleBasedNumberFormat fmt4 = new RuleBasedNumberFormat(fracRules, Locale.ENGLISH);
    RuleBasedNumberFormat fmt5 = new RuleBasedNumberFormat(fracRules, Locale.ENGLISH);
    str = fmt4.toString();
    logln(str);
    if (!fmt4.equals(fmt5)) {
        errln("duplicate 2 equality failed");
    }
    str = fmt4.format(123L);
    logln(str);
    try {
        Number num = fmt4.parse(str);
        logln(num.toString());
    } catch (Exception e) {
        errln("parse caught exception");
    }
    str = fmt4.format(.000123);
    logln(str);
    try {
        Number num = fmt4.parse(str);
        logln(num.toString());
    } catch (Exception e) {
        errln("parse caught exception");
    }
    str = fmt4.format(456.000123);
    logln(str);
    try {
        Number num = fmt4.parse(str);
        logln(num.toString());
    } catch (Exception e) {
        errln("parse caught exception");
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 23 with RuleBasedNumberFormat

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

the class RbnfTest method TestThaiSpellout.

/**
 * Perform a simple spot check on the Thai spellout rules
 */
@Test
public void TestThaiSpellout() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(new Locale("th", "TH"), RuleBasedNumberFormat.SPELLOUT);
    String[][] testData = { { "0", "\u0e28\u0e39\u0e19\u0e22\u0e4c" }, { "1", "\u0e2b\u0e19\u0e36\u0e48\u0e07" }, { "10", "\u0e2a\u0e34\u0e1a" }, { "11", "\u0e2a\u0e34\u0e1a\u200b\u0e40\u0e2d\u0e47\u0e14" }, { "21", "\u0e22\u0e35\u0e48\u200b\u0e2a\u0e34\u0e1a\u200b\u0e40\u0e2d\u0e47\u0e14" }, { "101", "\u0e2b\u0e19\u0e36\u0e48\u0e07\u200b\u0e23\u0e49\u0e2d\u0e22\u200b\u0e2b\u0e19\u0e36\u0e48\u0e07" }, { "1.234", "\u0e2b\u0e19\u0e36\u0e48\u0e07\u200b\u0e08\u0e38\u0e14\u200b\u0e2a\u0e2d\u0e07\u0e2a\u0e32\u0e21\u0e2a\u0e35\u0e48" }, { "21.45", "\u0e22\u0e35\u0e48\u200b\u0e2a\u0e34\u0e1a\u200b\u0e40\u0e2d\u0e47\u0e14\u200b\u0e08\u0e38\u0e14\u200b\u0e2a\u0e35\u0e48\u0e2b\u0e49\u0e32" }, { "22.45", "\u0e22\u0e35\u0e48\u200b\u0e2a\u0e34\u0e1a\u200b\u0e2a\u0e2d\u0e07\u200b\u0e08\u0e38\u0e14\u200b\u0e2a\u0e35\u0e48\u0e2b\u0e49\u0e32" }, { "23.45", "\u0e22\u0e35\u0e48\u200b\u0e2a\u0e34\u0e1a\u200b\u0e2a\u0e32\u0e21\u200b\u0e08\u0e38\u0e14\u200b\u0e2a\u0e35\u0e48\u0e2b\u0e49\u0e32" }, { "123.45", "\u0e2b\u0e19\u0e36\u0e48\u0e07\u200b\u0e23\u0e49\u0e2d\u0e22\u200b\u0e22\u0e35\u0e48\u200b\u0e2a\u0e34\u0e1a\u200b\u0e2a\u0e32\u0e21\u200b\u0e08\u0e38\u0e14\u200b\u0e2a\u0e35\u0e48\u0e2b\u0e49\u0e32" }, { "12,345.678", "\u0E2B\u0E19\u0E36\u0E48\u0E07\u200b\u0E2B\u0E21\u0E37\u0E48\u0E19\u200b\u0E2A\u0E2D\u0E07\u200b\u0E1E\u0E31\u0E19\u200b\u0E2A\u0E32\u0E21\u200b\u0E23\u0E49\u0E2D\u0E22\u200b\u0E2A\u0E35\u0E48\u200b\u0E2A\u0E34\u0E1A\u200b\u0E2B\u0E49\u0E32\u200b\u0E08\u0E38\u0E14\u200b\u0E2B\u0E01\u0E40\u0E08\u0E47\u0E14\u0E41\u0E1B\u0E14" } };
    doTest(formatter, testData, true);
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 24 with RuleBasedNumberFormat

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

the class RbnfTest method TestTrailingSemicolon.

@Test
public void TestTrailingSemicolon() {
    String thaiRules = "%default:\n" + "  -x: \u0e25\u0e1a>>;\n" + "  x.x: <<\u0e08\u0e38\u0e14>>>;\n" + "  \u0e28\u0e39\u0e19\u0e22\u0e4c; \u0e2b\u0e19\u0e36\u0e48\u0e07; \u0e2a\u0e2d\u0e07; \u0e2a\u0e32\u0e21;\n" + "  \u0e2a\u0e35\u0e48; \u0e2b\u0e49\u0e32; \u0e2b\u0e01; \u0e40\u0e08\u0e47\u0e14; \u0e41\u0e1b\u0e14;\n" + "  \u0e40\u0e01\u0e49\u0e32; \u0e2a\u0e34\u0e1a; \u0e2a\u0e34\u0e1a\u0e40\u0e2d\u0e47\u0e14;\n" + "  \u0e2a\u0e34\u0e1a\u0e2a\u0e2d\u0e07; \u0e2a\u0e34\u0e1a\u0e2a\u0e32\u0e21;\n" + "  \u0e2a\u0e34\u0e1a\u0e2a\u0e35\u0e48; \u0e2a\u0e34\u0e1a\u0e2b\u0e49\u0e32;\n" + "  \u0e2a\u0e34\u0e1a\u0e2b\u0e01; \u0e2a\u0e34\u0e1a\u0e40\u0e08\u0e47\u0e14;\n" + "  \u0e2a\u0e34\u0e1a\u0e41\u0e1b\u0e14; \u0e2a\u0e34\u0e1a\u0e40\u0e01\u0e49\u0e32;\n" + "  20: \u0e22\u0e35\u0e48\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  30: \u0e2a\u0e32\u0e21\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  40: \u0e2a\u0e35\u0e48\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  50: \u0e2b\u0e49\u0e32\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  60: \u0e2b\u0e01\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  70: \u0e40\u0e08\u0e47\u0e14\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  80: \u0e41\u0e1b\u0e14\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  90: \u0e40\u0e01\u0e49\u0e32\u0e2a\u0e34\u0e1a[>%%alt-ones>];\n" + "  100: <<\u0e23\u0e49\u0e2d\u0e22[>>];\n" + "  1000: <<\u0e1e\u0e31\u0e19[>>];\n" + "  10000: <<\u0e2b\u0e21\u0e37\u0e48\u0e19[>>];\n" + "  100000: <<\u0e41\u0e2a\u0e19[>>];\n" + "  1,000,000: <<\u0e25\u0e49\u0e32\u0e19[>>];\n" + "  1,000,000,000: <<\u0e1e\u0e31\u0e19\u0e25\u0e49\u0e32\u0e19[>>];\n" + "  1,000,000,000,000: <<\u0e25\u0e49\u0e32\u0e19\u0e25\u0e49\u0e32\u0e19[>>];\n" + "  1,000,000,000,000,000: =#,##0=;\n" + "%%alt-ones:\n" + "  \u0e28\u0e39\u0e19\u0e22\u0e4c;\n" + "  \u0e40\u0e2d\u0e47\u0e14;\n" + "  =%default=;\n ; ;; ";
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(thaiRules, new Locale("th", "TH", ""));
    String[][] testData = { { "0", "\u0e28\u0e39\u0e19\u0e22\u0e4c" }, { "1", "\u0e2b\u0e19\u0e36\u0e48\u0e07" }, { "123.45", "\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e23\u0e49\u0e2d\u0e22\u0e22\u0e35\u0e48\u0e2a\u0e34\u0e1a\u0e2a\u0e32\u0e21\u0e08\u0e38\u0e14\u0e2a\u0e35\u0e48\u0e2b\u0e49\u0e32" } };
    doTest(formatter, testData, true);
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 25 with RuleBasedNumberFormat

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

the class RbnfTest method TestSmallValues.

@Test
public void TestSmallValues() {
    String[][] testData = { { "0.001", "zero point zero zero one" }, { "0.0001", "zero point zero zero zero one" }, { "0.00001", "zero point zero zero zero zero one" }, { "0.000001", "zero point zero zero zero zero zero one" }, { "0.0000001", "zero point zero zero zero zero zero zero one" }, { "0.00000001", "zero point zero zero zero zero zero zero zero one" }, { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" }, { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" }, { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" }, { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" }, { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" }, { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" }, { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" }, { "10,000,000.001", "ten million point zero zero one" }, { "10,000,000.0001", "ten million point zero zero zero one" }, { "10,000,000.00001", "ten million point zero zero zero zero one" }, { "10,000,000.000001", "ten million point zero zero zero zero zero one" }, { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" }, { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" }, { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" }, { "10,000,000", "ten million" }, { "1,234,567,890.0987654", "one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety point zero nine eight seven six five four" }, { "123,456,789.9876543", "one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine point nine eight seven six five four three" }, { "12,345,678.87654321", "twelve million three hundred forty-five thousand six hundred seventy-eight point eight seven six five four three two one" }, { "1,234,567.7654321", "one million two hundred thirty-four thousand five hundred sixty-seven point seven six five four three two one" }, { "123,456.654321", "one hundred twenty-three thousand four hundred fifty-six point six five four three two one" }, { "12,345.54321", "twelve thousand three hundred forty-five point five four three two one" }, { "1,234.4321", "one thousand two hundred thirty-four point four three two one" }, { "123.321", "one hundred twenty-three point three two one" }, { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" }, { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" } };
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.SPELLOUT);
    doTest(formatter, testData, true);
}
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