Search in sources :

Example 6 with RuleBasedNumberFormat

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

the class RbnfRoundTripTest method TestItalianSpelloutRT.

/**
 * Perform an exhaustive round-trip test on the Italian spellout rules
 */
@Test
public void TestItalianSpelloutRT() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.ITALIAN, RuleBasedNumberFormat.SPELLOUT);
    doTest(formatter, -999999, 999999);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 7 with RuleBasedNumberFormat

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

the class RbnfRoundTripTest method TestSwissFrenchSpelloutRT.

/**
 * Perform an exhaustive round-trip test on the Swiss French spellout rules
 */
@Test
public void TestSwissFrenchSpelloutRT() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(new Locale("fr", "CH", ""), RuleBasedNumberFormat.SPELLOUT);
    doTest(formatter, -12345678, 12345678);
}
Also used : Locale(java.util.Locale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 8 with RuleBasedNumberFormat

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

the class RbnfLenientScannerTest method doLenientParseTest.

void doLenientParseTest(RuleBasedNumberFormat formatter, String[][] testData) {
    NumberFormat decFmt = NumberFormat.getInstance(Locale.US);
    try {
        for (int i = 0; i < testData.length; i++) {
            String words = testData[i][0];
            String expectedNumber = testData[i][1];
            String actualNumber = decFmt.format(formatter.parse(words));
            if (!actualNumber.equals(expectedNumber)) {
                errln("Lenient-parse spot check failed: for " + words + ", expected " + expectedNumber + ", but got " + actualNumber);
            }
        }
    } catch (Throwable e) {
        errln("Test failed with exception: " + e.toString());
        e.printStackTrace();
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(java.text.NumberFormat)

Example 9 with RuleBasedNumberFormat

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

the class RbnfTest method TestBigNumbers.

@Test
public void TestBigNumbers() {
    BigInteger bigI = new BigInteger("1234567890", 10);
    StringBuffer buf = new StringBuffer();
    RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(RuleBasedNumberFormat.SPELLOUT);
    fmt.format(bigI, buf, null);
    logln("big int: " + buf.toString());
    buf.setLength(0);
    java.math.BigDecimal bigD = new java.math.BigDecimal(bigI);
    fmt.format(bigD, buf, null);
    logln("big dec: " + buf.toString());
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) BigInteger(java.math.BigInteger) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 10 with RuleBasedNumberFormat

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

the class RbnfTest method TestPluralRules.

/**
 * Perform a simple spot check on the ordinal spellout rules
 */
@Test
public void TestPluralRules() {
    String enRules = "%digits-ordinal:" + "-x: −>>;" + "0: =#,##0=$(ordinal,one{st}two{nd}few{rd}other{th})$;";
    RuleBasedNumberFormat enFormatter = new RuleBasedNumberFormat(enRules, ULocale.ENGLISH);
    String[][] enTestData = { { "1", "1st" }, { "2", "2nd" }, { "3", "3rd" }, { "4", "4th" }, { "11", "11th" }, { "12", "12th" }, { "13", "13th" }, { "14", "14th" }, { "21", "21st" }, { "22", "22nd" }, { "23", "23rd" }, { "24", "24th" } };
    doTest(enFormatter, enTestData, true);
    // 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.
    String ruRules = "%spellout-numbering:" + "-x: минус >>;" + "x.x: [<< $(cardinal,one{целый}other{целых})$ ]>%%fractions-feminine>;" + "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: << $(cardinal,one{миллион}few{миллионы}other{миллионов})$[ >>];" + "%%fractions-feminine:" + "10: <%spellout-numbering< $(cardinal,one{десятая}other{десятых})$;" + "100: <%spellout-numbering< $(cardinal,one{сотая}other{сотых})$;";
    RuleBasedNumberFormat ruFormatter = new RuleBasedNumberFormat(ruRules, new ULocale("ru"));
    String[][] ruTestData = { { "1", "один" }, { "100", "сто" }, { "125", "сто двадцать пять" }, { "399", "триста девяносто девять" }, { "1,000", "один тысяча" }, { "1,001", "один тысяча один" }, { "2,000", "два тысячи" }, { "2,001", "два тысячи один" }, { "2,002", "два тысячи два" }, { "3,333", "три тысячи триста тридцать три" }, { "5,000", "пять тысяч" }, { "11,000", "одиннадцать тысяч" }, { "21,000", "двадцать один тысяча" }, { "22,000", "двадцать два тысячи" }, { "25,001", "двадцать пять тысяч один" }, { "0.1", "один десятая" }, { "0.2", "два десятых" }, { "0.21", "двадцать один сотая" }, { "0.22", "двадцать два сотых" }, { "21.1", "двадцать один целый один десятая" }, { "22.2", "двадцать два целых два десятых" } };
    doTest(ruFormatter, ruTestData, true);
    // Make sure there are no divide by 0 errors.
    String result = new RuleBasedNumberFormat(ruRules, new ULocale("ru")).format(21000);
    if (!"двадцать один тысяча".equals(result)) {
        errln("Got " + result + " for 21000");
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) ULocale(android.icu.util.ULocale) 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