Search in sources :

Example 51 with RuleBasedNumberFormat

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

the class RbnfRoundTripTest method TestGreekSpelloutRT.

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

Example 52 with RuleBasedNumberFormat

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

the class RbnfTest method TestFrenchSpellout.

/**
 * Perform a simple spot check on the French spellout rules
 */
@Test
public void TestFrenchSpellout() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.FRANCE, RuleBasedNumberFormat.SPELLOUT);
    String[][] testData = { { "1", "un" }, { "15", "quinze" }, { "20", "vingt" }, { "21", "vingt-et-un" }, { "23", "vingt-trois" }, { "62", "soixante-deux" }, { "70", "soixante-dix" }, { "71", "soixante-et-onze" }, { "73", "soixante-treize" }, { "80", "quatre-vingts" }, { "88", "quatre-vingt-huit" }, { "100", "cent" }, { "106", "cent six" }, { "127", "cent vingt-sept" }, { "200", "deux cents" }, { "579", "cinq cent soixante-dix-neuf" }, { "1,000", "mille" }, { "1,123", "mille cent vingt-trois" }, { "1,594", "mille cinq cent quatre-vingt-quatorze" }, { "2,000", "deux mille" }, { "3,004", "trois mille quatre" }, { "4,567", "quatre mille cinq cent soixante-sept" }, { "15,943", "quinze mille neuf cent quarante-trois" }, { "2,345,678", "deux millions trois cent quarante-cinq mille " + "six cent soixante-dix-huit" }, { "-36", "moins trente-six" }, { "234.567", "deux cent trente-quatre virgule cinq six sept" } };
    doTest(formatter, testData, true);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 53 with RuleBasedNumberFormat

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

the class RbnfTest method TestContext.

@Test
public void TestContext() {
    class TextContextItem {

        public String locale;

        public int format;

        public DisplayContext context;

        public double value;

        public String expectedResult;

        // Simple constructor
        public TextContextItem(String loc, int fmt, DisplayContext ctxt, double val, String expRes) {
            locale = loc;
            format = fmt;
            context = ctxt;
            value = val;
            expectedResult = expRes;
        }
    }
    final TextContextItem[] items = { new TextContextItem("sv", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, 123.45, "ett\u00ADhundra\u00ADtjugo\u00ADtre komma fyra fem"), new TextContextItem("sv", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, 123.45, "Ett\u00ADhundra\u00ADtjugo\u00ADtre komma fyra fem"), new TextContextItem("sv", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, 123.45, "ett\u00ADhundra\u00ADtjugo\u00ADtre komma fyra fem"), new TextContextItem("sv", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_STANDALONE, 123.45, "ett\u00ADhundra\u00ADtjugo\u00ADtre komma fyra fem"), new TextContextItem("en", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, 123.45, "one hundred twenty-three point four five"), new TextContextItem("en", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, 123.45, "One hundred twenty-three point four five"), new TextContextItem("en", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, 123.45, "One hundred twenty-three point four five"), new TextContextItem("en", RuleBasedNumberFormat.SPELLOUT, DisplayContext.CAPITALIZATION_FOR_STANDALONE, 123.45, "One hundred twenty-three point four five") };
    for (TextContextItem item : items) {
        ULocale locale = new ULocale(item.locale);
        RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(locale, item.format);
        rbnf.setContext(item.context);
        String result = rbnf.format(item.value, rbnf.getDefaultRuleSetName());
        if (!result.equals(item.expectedResult)) {
            errln("Error for locale " + item.locale + ", context " + item.context + ", expected " + item.expectedResult + ", got " + result);
        }
        RuleBasedNumberFormat rbnfClone = (RuleBasedNumberFormat) rbnf.clone();
        if (!rbnfClone.equals(rbnf)) {
            errln("Error for locale " + item.locale + ", context " + item.context + ", rbnf.clone() != rbnf");
        } else {
            result = rbnfClone.format(item.value, rbnfClone.getDefaultRuleSetName());
            if (!result.equals(item.expectedResult)) {
                errln("Error with clone for locale " + item.locale + ", context " + item.context + ", expected " + item.expectedResult + ", got " + result);
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) DisplayContext(android.icu.text.DisplayContext) Test(org.junit.Test)

Example 54 with RuleBasedNumberFormat

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

the class RbnfTest method TestItalianSpellout.

/**
 * Perform a simple spot check on the Italian spellout rules
 */
@Test
public void TestItalianSpellout() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.ITALIAN, RuleBasedNumberFormat.SPELLOUT);
    String[][] testData = { { "1", "uno" }, { "15", "quindici" }, { "20", "venti" }, { "23", "venti\u00ADtr\u00E9" }, { "73", "settanta\u00ADtr\u00E9" }, { "88", "ottant\u00ADotto" }, { "100", "cento" }, { "106", "cento\u00ADsei" }, { "108", "cent\u00ADotto" }, { "127", "cento\u00ADventi\u00ADsette" }, { "181", "cent\u00ADottant\u00ADuno" }, { "200", "due\u00ADcento" }, { "579", "cinque\u00ADcento\u00ADsettanta\u00ADnove" }, { "1,000", "mille" }, { "2,000", "due\u00ADmila" }, { "3,004", "tre\u00ADmila\u00ADquattro" }, { "4,567", "quattro\u00ADmila\u00ADcinque\u00ADcento\u00ADsessanta\u00ADsette" }, { "15,943", "quindici\u00ADmila\u00ADnove\u00ADcento\u00ADquaranta\u00ADtr\u00E9" }, { "-36", "meno trenta\u00ADsei" }, { "234.567", "due\u00ADcento\u00ADtrenta\u00ADquattro virgola cinque sei sette" } };
    doTest(formatter, testData, true);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 55 with RuleBasedNumberFormat

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

the class RbnfTest method TestVariableDecimalPoint.

@Test
public void TestVariableDecimalPoint() {
    String enRules = "%spellout-numbering:" + "-x: minus >>;" + "x.x: << point >>;" + "x,x: << comma >>;" + "0.x: xpoint >>;" + "0,x: xcomma >>;" + "0: zero;" + "1: one;" + "2: two;" + "3: three;" + "4: four;" + "5: five;" + "6: six;" + "7: seven;" + "8: eight;" + "9: nine;";
    RuleBasedNumberFormat enFormatter = new RuleBasedNumberFormat(enRules, ULocale.ENGLISH);
    String[][] enTestPointData = { { "1.1", "one point one" }, { "1.23", "one point two three" }, { "0.4", "xpoint four" } };
    doTest(enFormatter, enTestPointData, true);
    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(ULocale.ENGLISH);
    decimalFormatSymbols.setDecimalSeparator(',');
    enFormatter.setDecimalFormatSymbols(decimalFormatSymbols);
    String[][] enTestCommaData = { { "1.1", "one comma one" }, { "1.23", "one comma two three" }, { "0.4", "xcomma four" } };
    doTest(enFormatter, enTestCommaData, true);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) 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