Java Code Examples for android.icu.text.NumberFormat#getInstance()

The following examples show how to use android.icu.text.NumberFormat#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DecimalFormat.parse() fails for mulipliers 2^n.
 */
@Test
public void Test4216742() throws ParseException {
    DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    long[] DATA = { Long.MIN_VALUE, Long.MAX_VALUE, -100000000L, 100000000L};
    for (int i=0; i<DATA.length; ++i) {
        String str = Long.toString(DATA[i]);
        for (int m = 1; m <= 100; m++) {
            fmt.setMultiplier(m);
            long n = ((Number) fmt.parse(str)).longValue();
            if (n > 0 != DATA[i] > 0) {
                errln("\"" + str + "\" parse(x " + fmt.getMultiplier() +
                      ") => " + n);
            }
        }
    }
}
 
Example 2
Source File: PluralFormatUnitTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestSetLocale() {
    // Create rules for testing.
    PluralRules oddAndEven = PluralRules.createRules("odd__: n mod 2 is 1");

    PluralFormat plFmt = new PluralFormat(oddAndEven);
    plFmt.applyPattern("odd__{odd} other{even}");
    plFmt.setLocale(ULocale.ENGLISH);

    // Check that pattern gets deleted.
    NumberFormat nrFmt = NumberFormat.getInstance(ULocale.ENGLISH);
    assertEquals("pattern was not resetted by setLocale() call.",
            nrFmt.format(5),
            plFmt.format(5));

    // Check that rules got updated.
    plFmt.applyPattern("odd__{odd} other{even}");
    assertEquals("SetLocale should reset rules but did not.", "even", plFmt.format(1));

    plFmt.applyPattern("one{one} other{not one}");
    for (int i = 0; i < 20; ++i) {
        assertEquals("Wrong ruleset loaded by setLocale()",
                ((i==1) ? "one" : "not one"),
                plFmt.format(i));
    }
}
 
Example 3
Source File: IntlTestNumberFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * call _testFormat for currency, percent and plain number instances
 */
@Test
public void TestLocale() {
    Locale locale = Locale.getDefault();
    String localeName = locale + " (" + locale.getDisplayName() + ")";
        
    logln("Number test " + localeName);
    fNumberFormat = NumberFormat.getInstance(locale);
    _testFormat();

    logln("Currency test " + localeName);
    fNumberFormat = NumberFormat.getCurrencyInstance(locale);
    _testFormat();

    logln("Percent test " + localeName);
    fNumberFormat = NumberFormat.getPercentInstance(locale);
    _testFormat();
}
 
Example 4
Source File: IntlTestNumberFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Internal use
 */
private void _testLocale(Locale locale) {
    String localeName = locale + " (" + locale.getDisplayName() + ")";
        
    logln("Number test " + localeName);
    fNumberFormat = NumberFormat.getInstance(locale);
    _testFormat();

    logln("Currency test " + localeName);
    fNumberFormat = NumberFormat.getCurrencyInstance(locale);
    _testFormat();

    logln("Percent test " + localeName);
    fNumberFormat = NumberFormat.getPercentInstance(locale);
    _testFormat();

    if (locale.toString().compareTo("en_US_POSIX") != 0 ) {
       logln("Scientific test " + localeName);
       fNumberFormat = NumberFormat.getScientificInstance(locale);
       _testFormat();
    }
}
 
Example 5
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * NumberFormat does not parse negative zero.
 */
@Test
public void Test4162852() throws ParseException {
    for (int i=0; i<2; ++i) {
        NumberFormat f = (i == 0) ? NumberFormat.getInstance()
            : NumberFormat.getPercentInstance();
        double d = -0.0;
        String s = f.format(d);
        double e = f.parse(s).doubleValue();
        logln("" +
              d + " -> " +
              '"' + s + '"' + " -> " +
          e);
        if (e != 0.0 || 1.0/e > 0.0) {
            logln("Failed to parse negative zero");
        }
    }
}
 
Example 6
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction
 * digits.
 */
@Test
public void Test4217661() {
    Object[] DATA = {
        new Double(0.001), "0",
        new Double(1.001), "1",
        new Double(0.006), "0.01",
        new Double(1.006), "1.01",
    };
    NumberFormat fmt = NumberFormat.getInstance(Locale.US);
    fmt.setMaximumFractionDigits(2); 
    for (int i=0; i<DATA.length; i+=2) {
        String s = fmt.format(((Double) DATA[i]).doubleValue());
        if (!s.equals(DATA[i+1])) {
            errln("FAIL: Got " + s + ", exp " + DATA[i+1]); 
        }
    }
}
 
Example 7
Source File: PluralRulesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestBasicFraction() {
    String[][] tests = { { "en", "one: j is 1" }, { "1", "0", "1", "one" }, { "1", "2", "1.00", "other" }, };
    ULocale locale = null;
    NumberFormat nf = null;
    PluralRules pr = null;

    for (String[] row : tests) {
        switch (row.length) {
        case 2:
            locale = ULocale.forLanguageTag(row[0]);
            nf = NumberFormat.getInstance(locale);
            pr = PluralRules.createRules(row[1]);
            break;
        case 4:
            double n = Double.parseDouble(row[0]);
            int minFracDigits = Integer.parseInt(row[1]);
            nf.setMinimumFractionDigits(minFracDigits);
            String expectedFormat = row[2];
            String expectedKeyword = row[3];

            UFieldPosition pos = new UFieldPosition();
            String formatted = nf.format(1.0, new StringBuffer(), pos).toString();
            int countVisibleFractionDigits = pos.getCountVisibleFractionDigits();
            long fractionDigits = pos.getFractionDigits();
            String keyword = pr.select(n, countVisibleFractionDigits, fractionDigits);
            assertEquals("Formatted " + n + "\t" + minFracDigits, expectedFormat, formatted);
            assertEquals("Keyword " + n + "\t" + minFracDigits, expectedKeyword, keyword);
            break;
        default:
            throw new RuntimeException();
        }
    }
}
 
Example 8
Source File: NumberFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * alphaWorks upgrade
 */
@Test
public void Test4161100() {
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    nf.setMinimumFractionDigits(1);
    nf.setMaximumFractionDigits(1);
    double a = -0.09;
    String s = nf.format(a);
    logln(a + " x " +
          ((DecimalFormat) nf).toPattern() + " = " + s);
    if (!s.equals("-0.1")) {
        errln("FAIL");
    }
}
 
Example 9
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4161100() {
    NumberFormat f = NumberFormat.getInstance();
    f.setMinimumFractionDigits(1);
    f.setMaximumFractionDigits(1);
    double a = -0.09;
    String s = f.format(a);
    logln(a + " x " +
          ((DecimalFormat) f).toPattern() + " = " +
          s);
    if (!s.equals("-0.1")) {
        errln("FAIL");
    }
}
 
Example 10
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * DecimalFormat.setMaximumIntegerDigits() works incorrectly.
 */
@Test
public void Test4110936()
{
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumIntegerDigits(128);
    logln("setMaximumIntegerDigits(128)");
    if (nf.getMaximumIntegerDigits() != 128)
        errln("getMaximumIntegerDigits() returns " +
            nf.getMaximumIntegerDigits());
}
 
Example 11
Source File: NumberFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestSurrogatesParsing() { // Test parsing of numbers that use digits from the supplemental planes.
    final String[] data = {
            "1\ud801\udca2,3\ud801\udca45.67", // 
            "\ud801\udca1\ud801\udca2,\ud801\udca3\ud801\udca4\ud801\udca5.\ud801\udca6\ud801\udca7\ud801\udca8", // 
            "\ud835\udfd2.\ud835\udfd7E-\ud835\udfd1",
            "\ud835\udfd3.8E-0\ud835\udfd0"
            };
    final double[] expected = {
            12345.67,
            12345.678,
            0.0049,
            0.058
    };

    NumberFormat nfmt = NumberFormat.getInstance();

    for (int i = 0; i < data.length; i++) {
        try {
            Number n = nfmt.parse(data[i]);
            if (expected[i] != n.doubleValue()) {
                errln("Failed: Parsed result for " + data[i] + ": " 
                        + n.doubleValue() + " / expected: " + expected[i]);
            }
        } catch (ParseException pe) {
            errln("Failed: ParseException is thrown for " + data[i]);
        }
    }
}
 
Example 12
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * NumberFormat.parse doesn't return null
 */
@Test
public void Test4114639()
{
    NumberFormat format = NumberFormat.getInstance();
    String text = "time 10:x";
    ParsePosition pos = new ParsePosition(8);
    Number result = format.parse(text, pos);
    if (result != null) errln("Should return null but got : " + result); // Should be null; it isn't
}
 
Example 13
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * DecimalFormat does not round up correctly.
 */
@Test
public void Test4071492 (){
    double x = 0.00159999;
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    String out = nf.format(x);
    logln("0.00159999 formats with 4 fractional digits to " + out);
    String expected = "0.0016";
    if (!out.equals(expected))
        errln("FAIL: Expected " + expected);
}
 
Example 14
Source File: ScientificNumberFormatterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestFixedDecimalMarkup() {
    DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(ULocale.ENGLISH);
    ScientificNumberFormatter fmt = ScientificNumberFormatter.getMarkupInstance(
            decfmt, "<sup>", "</sup>");
    assertEquals(
            "",
            "123,456",
            fmt.format(123456.0));
}
 
Example 15
Source File: ScientificNumberFormatterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestFixedDecimalSuperscript() {
    DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(ULocale.ENGLISH);
    ScientificNumberFormatter fmt = ScientificNumberFormatter.getSuperscriptInstance(decfmt);
    assertEquals(
            "",
            "123,456",
            fmt.format(123456.0));
}
 
Example 16
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * NumberFormat truncates data
 */
@Test
public void Test4167494() throws Exception {
    NumberFormat fmt = NumberFormat.getInstance(Locale.US);
    
    double a = Double.MAX_VALUE;
    String s = fmt.format(a);
    double b = fmt.parse(s).doubleValue();
    boolean match = a == b;
    if (match) {
        logln("" + a + " -> \"" + s + "\" -> " + b + " ok");
    } else {
        errln("" + a + " -> \"" + s + "\" -> " + b + " FAIL");
    }

    // We don't test Double.MIN_VALUE because the locale data for the US
    // currently doesn't specify enough digits to display Double.MIN_VALUE.
    // This is correct for now; however, we leave this here as a reminder
    // in case we want to address this later.
    if (false) {
        a = Double.MIN_VALUE;
        s = fmt.format(a);
        b = fmt.parse(s).doubleValue();
        match = a == b;
        if (match) {
            logln("" + a + " -> \"" + s + "\" -> " + b + " ok");
        } else {
            errln("" + a + " -> \"" + s + "\" -> " + b + " FAIL");
        }
    }
}
 
Example 17
Source File: RbnfTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestUndefinedSpellout() {
    Locale greek = new Locale("el", "", "");
    RuleBasedNumberFormat[] formatters = {
            new RuleBasedNumberFormat(greek, RuleBasedNumberFormat.SPELLOUT),
            new RuleBasedNumberFormat(greek, RuleBasedNumberFormat.ORDINAL),
            new RuleBasedNumberFormat(greek, RuleBasedNumberFormat.DURATION),
    };

    String[] data = {
            "0",
            "1",
            "15",
            "20",
            "23",
            "73",
            "88",
            "100",
            "106",
            "127",
            "200",
            "579",
            "1,000",
            "2,000",
            "3,004",
            "4,567",
            "15,943",
            "105,000",
            "2,345,678",
            "-36",
            "-36.91215",
            "234.56789"
    };

    NumberFormat decFormat = NumberFormat.getInstance(Locale.US);
    for (int j = 0; j < formatters.length; ++j) {
        android.icu.text.NumberFormat formatter = formatters[j];
        logln("formatter[" + j + "]");
        for (int i = 0; i < data.length; ++i) {
            try {
                String result = formatter.format(decFormat.parse(data[i]));
                logln("[" + i + "] " + data[i] + " ==> " + result);
            }
            catch (Exception e) {
                errln("formatter[" + j + "], data[" + i + "] " + data[i] + " threw exception " + e.getMessage());
            }
        }
    }
}
 
Example 18
Source File: TestMessageFormat.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestBug3()
{
    double myNumber = -123456;
    DecimalFormat form = null;
    Locale locale[] = {
        new Locale("ar", "", ""),
        new Locale("be", "", ""),
        new Locale("bg", "", ""),
        new Locale("ca", "", ""),
        new Locale("cs", "", ""),
        new Locale("da", "", ""),
        new Locale("de", "", ""),
        new Locale("de", "AT", ""),
        new Locale("de", "CH", ""),
        new Locale("el", "", ""),       // 10
        new Locale("en", "CA", ""),
        new Locale("en", "GB", ""),
        new Locale("en", "IE", ""),
        new Locale("en", "US", ""),
        new Locale("es", "", ""),
        new Locale("et", "", ""),
        new Locale("fi", "", ""),
        new Locale("fr", "", ""),
        new Locale("fr", "BE", ""),
        new Locale("fr", "CA", ""),     // 20
        new Locale("fr", "CH", ""),
        new Locale("he", "", ""),
        new Locale("hr", "", ""),
        new Locale("hu", "", ""),
        new Locale("is", "", ""),
        new Locale("it", "", ""),
        new Locale("it", "CH", ""),
        new Locale("ja", "", ""),
        new Locale("ko", "", ""),
        new Locale("lt", "", ""),       // 30
        new Locale("lv", "", ""),
        new Locale("mk", "", ""),
        new Locale("nl", "", ""),
        new Locale("nl", "BE", ""),
        new Locale("no", "", ""),
        new Locale("pl", "", ""),
        new Locale("pt", "", ""),
        new Locale("ro", "", ""),
        new Locale("ru", "", ""),
        new Locale("sh", "", ""),       // 40
        new Locale("sk", "", ""),
        new Locale("sl", "", ""),
        new Locale("sq", "", ""),
        new Locale("sr", "", ""),
        new Locale("sv", "", ""),
        new Locale("tr", "", ""),
        new Locale("uk", "", ""),
        new Locale("zh", "", ""),
        new Locale("zh", "TW", "")      // 49
    };
    StringBuffer buffer = new StringBuffer();
    ParsePosition parsePos = new ParsePosition(0);
    int i;
    for (i= 0; i < 49; i++) {
//        form = (DecimalFormat)NumberFormat.getCurrencyInstance(locale[i]);
        form = (DecimalFormat)NumberFormat.getInstance(locale[i]);
        if (form == null) {
            errln("Number format creation failed for " + locale[i].getDisplayName());
            continue;
        }
        FieldPosition pos = new FieldPosition(0);
        buffer.setLength(0);
        form.format(myNumber, buffer, pos);
        parsePos.setIndex(0);
        Object result = form.parse(buffer.toString(), parsePos);
        logln(locale[i].getDisplayName() + " -> " + result);
        if (parsePos.getIndex() != buffer.length()) {
            errln("Number format parse failed.");
        }
    }
}
 
Example 19
Source File: TestCLDRVsICU.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void handleResult(ULocale locale, String result) {
    NumberFormat nf = null;
    double v = Double.NaN;
    for (Iterator it = settings.keySet().iterator(); it.hasNext();) {
        String attributeName = (String) it.next();
        String attributeValue = (String) settings.get(attributeName);

        // Checks if the attribute name is a draft and whether
        // or not it has been approved / contributed by CLDR yet
        // otherwise, skips it because it is most likely rejected by ICU
        if (attributeName.equals("draft")) {
            if (attributeValue.indexOf("approved") == -1 && attributeValue.indexOf("contributed") == -1) {
                break;
            }
            continue;
        }

        // Update the value to be checked
        if (attributeName.equals("input")) {
            v = Double.parseDouble(attributeValue);
            continue;
        }

        // At this point, it must be a numberType
        int index = lookupValue(attributeValue, NumberNames);

        if (DEBUG)
            logln("Getting number format for " + locale);
        switch (index) {
        case 0:
            nf = NumberFormat.getInstance(locale);
            break;
        case 1:
            nf = NumberFormat.getIntegerInstance(locale);
            break;
        case 2:
            nf = NumberFormat.getNumberInstance(locale);
            break;
        case 3:
            nf = NumberFormat.getPercentInstance(locale);
            break;
        case 4:
            nf = NumberFormat.getScientificInstance(locale);
            break;
        default:
            nf = NumberFormat.getCurrencyInstance(locale);
            nf.setCurrency(Currency.getInstance(attributeValue));
            break;
        }
        String temp = nf.format(v).trim();
        result = result.trim(); // HACK because of SAX
        if (!temp.equals(result)) {
            logln("Number: Locale: " + locale +
                    "\n\tType: " + attributeValue +
                    "\n\tDraft: " + settings.get("draft") +
                    "\n\tCLDR: <" + result + ">" +
                    "\n\tICU: <" + temp + ">");
        }

    }
}
 
Example 20
Source File: TimeUnitTest.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * @bug9042 
 * Performs tests for Greek.
 * This tests that if the plural count listed in time unit format does not 
 * match those in the plural rules for the locale, those plural count in 
 * time unit format will be ingored and subsequently, fall back will kick in 
 * which is tested above. 
 * Without data sanitization, setNumberFormat() would crash. 
 * As of CLDR shiped in ICU4.8, Greek is one such language. 
 */ 
@Test
public void TestGreekWithSanitization() {
    ULocale loc = new ULocale("el");
    NumberFormat numfmt = NumberFormat.getInstance(loc);
    TimeUnitFormat tuf = new TimeUnitFormat(loc);
    tuf.parseObject("", new ParsePosition(0));
    tuf.setNumberFormat(numfmt);        
}