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

The following examples show how to use android.icu.text.NumberFormat#getNumberInstance() . 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: MeasureUnitTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test10219FractionalPlurals() {
    double[] values = {1.588, 1.011};
    String[][] expected = {
            {"1 minute", "1.5 minutes", "1.58 minutes"},
            {"1 minute", "1.0 minutes", "1.01 minutes"}
    };
    for (int j = 0; j < values.length; j++) {
        for (int i = 0; i < expected[j].length; i++) {
            NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
            nf.setRoundingMode(BigDecimal.ROUND_DOWN);
            nf.setMinimumFractionDigits(i);
            nf.setMaximumFractionDigits(i);
            MeasureFormat mf = MeasureFormat.getInstance(
                    ULocale.ENGLISH, FormatWidth.WIDE, nf);
            assertEquals("Test10219", expected[j][i], mf.format(new Measure(values[j], MeasureUnit.MINUTE)));
        }
    }
}
 
Example 2
Source File: TimeUnitTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test10219FractionalPlurals() {
    TimeUnitFormat tuf = new TimeUnitFormat(ULocale.ENGLISH, TimeUnitFormat.FULL_NAME);
    String[] expected = {"1 minute", "1.5 minutes", "1.58 minutes"};
    for (int i = 2; i >= 0; i--) {
        NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
        nf.setRoundingMode(BigDecimal.ROUND_DOWN);
        nf.setMaximumFractionDigits(i);
        tuf.setNumberFormat(nf);
        assertEquals("Test10219", expected[i], tuf.format(new TimeUnitAmount(1.588, TimeUnit.MINUTE)));
    }   
}
 
Example 3
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Number format data rounding errors for locale FR
 */
@Test
public void Test4070798 () {
    NumberFormat formatter;
    String tempString;
    /* User error :
    String expectedDefault = "-5\u00a0789,987";
    String expectedCurrency = "5\u00a0789,98\u00a0F";
    String expectedPercent = "-578\u00a0998%";
    */
    String expectedDefault = "-5\u00a0789,988";
    String expectedCurrency = "5\u00a0789,99\u00a0" + EURO; // euro
    String expectedPercent = "-578\u00a0999\u00a0%";

    formatter = NumberFormat.getNumberInstance(Locale.FRANCE);
    tempString = formatter.format (-5789.9876);

    if (tempString.equals(expectedDefault)) {
        logln ("Bug 4070798 default test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedDefault +
        " Received " + tempString );
    }


    formatter = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    tempString = formatter.format( 5789.9876 );

    if (tempString.equals(expectedCurrency) ) {
        logln ("Bug 4070798 currency test assed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedCurrency +
        " Received " + tempString );
    }


    formatter = NumberFormat.getPercentInstance(Locale.FRANCE);
    tempString = formatter.format (-5789.9876);

    if (tempString.equals(expectedPercent) ) {
        logln ("Bug 4070798 percentage test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedPercent +
        " Received " + tempString );
    }
}
 
Example 4
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Data rounding errors for French (Canada) locale
 */
@Test
public void Test4071005 () {

    NumberFormat formatter;
    String tempString;
/* user error :
    String expectedDefault = "-5 789,987";
    String expectedCurrency = "5 789,98\u00a0$";
    String expectedPercent = "-578 998%";
*/
    String expectedDefault = "-5\u00a0789,988";
    String expectedCurrency = "5\u00a0789,99\u00a0$";
    String expectedPercent = "-578\u00a0999\u00A0%";

    formatter = NumberFormat.getNumberInstance(Locale.CANADA_FRENCH);
    tempString = formatter.format (-5789.9876);
    if (tempString.equals(expectedDefault)) {
        logln ("Bug 4071005 default test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedDefault +
        " Received " + tempString );
    }

    formatter = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
    tempString = formatter.format( 5789.9876 ) ;

    if (tempString.equals(expectedCurrency) ) {
        logln ("Bug 4071005 currency test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedCurrency +
        " Received " + tempString );
    }
    formatter = NumberFormat.getPercentInstance(Locale.CANADA_FRENCH);
    tempString = formatter.format (-5789.9876);

    if (tempString.equals(expectedPercent) ) {
        logln ("Bug 4071005 percentage test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedPercent +
        " Received " + tempString );
    }
}
 
Example 5
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Data rounding errors for German (Germany) locale
 */
@Test
public void Test4071014 () {
    NumberFormat formatter;
    String tempString;
    /* user error :
    String expectedDefault = "-5.789,987";
    String expectedCurrency = "5.789,98\u00a0DM";
    String expectedPercent = "-578.998%";
    */
    String expectedDefault = "-5.789,988";
    String expectedCurrency = "5.789,99\u00a0" + EURO;
    String expectedPercent = "-578.999\u00a0%";

    formatter = NumberFormat.getNumberInstance(Locale.GERMANY);
    tempString = formatter.format (-5789.9876);

    if (tempString.equals(expectedDefault)) {
        logln ("Bug 4071014 default test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedDefault +
        " Received " + tempString );
    }

    formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY);
    tempString = formatter.format( 5789.9876 ) ;

    if (tempString.equals(expectedCurrency) ) {
        logln ("Bug 4071014 currency test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedCurrency +
        " Received " + tempString );
    }

    formatter = NumberFormat.getPercentInstance(Locale.GERMANY);
    tempString = formatter.format (-5789.9876);

    if (tempString.equals(expectedPercent) ) {
        logln ("Bug 4071014 percentage test passed.");
    } else {
        errln("Failed:" +
        " Expected " + expectedPercent +
        " Received " + tempString );
    }

}
 
Example 6
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Data rounding errors for Italian locale number formats
 * Note- with the Euro, there is no need for currency rounding anymore
 */
@Test
public void Test4071859 () {
    NumberFormat formatter;
    String tempString;
    /* user error :
    String expectedDefault = "-5.789,987";
    String expectedCurrency = "-L.\u00a05.789,98";
    String expectedPercent = "-578.998%";
    */
    String expectedDefault = "-5.789,988";
    String expectedCurrency = "-5.789,99\u00A0" + EURO;
    String expectedPercent = "-578.999%";

    formatter = NumberFormat.getNumberInstance(Locale.ITALY);
    tempString = formatter.format (-5789.9876);

    if (tempString.equals(expectedDefault)) {
        logln ("Bug 4071859 default test passed.");
    } else {
        errln("a) Failed:" +
        " Expected " + expectedDefault +
        " Received " + tempString );
    }

    formatter = NumberFormat.getCurrencyInstance(Locale.ITALY);
    tempString = formatter.format( -5789.9876 ) ;

    if (tempString.equals(expectedCurrency) ) {
        logln ("Bug 4071859 currency test passed.");
    } else {
        errln("b) Failed:" +
        " Expected " + expectedCurrency +
        " Received " + tempString );
    }

    formatter = NumberFormat.getPercentInstance(Locale.ITALY);
    tempString = formatter.format (-5789.9876);

    if (tempString.equals(expectedPercent) ) {
        logln ("Bug 4071859 percentage test passed.");
    } else {
        errln("c) Failed:" +
        " Expected " + expectedPercent +
        " Received " + tempString );
    }

}
 
Example 7
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 + ">");
        }

    }
}