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

The following examples show how to use android.icu.text.NumberFormat#getCurrencyInstance() . 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: NumberFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestNBSPInPattern() {
NumberFormat nf = null;
String testcase;


testcase="ar_AE UNUM_CURRENCY";
nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE"));
checkNBSPPatternRT(testcase, nf);
// if we don't have CLDR 1.6 data, bring out the problem anyways 

String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00";
testcase = "ar_AE special pattern: " + SPECIAL_PATTERN;
nf = new DecimalFormat();
((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN);
checkNBSPPatternRT(testcase, nf);

}
 
Example 2
Source File: NumberFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestT9293() {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    fmt.setParseStrict(true);

    final int val = 123456;
    String txt = fmt.format(123456);

    ParsePosition pos = new ParsePosition(0);
    Number num = fmt.parse(txt, pos);

    if (pos.getErrorIndex() >= 0) {
        errln("FAIL: Parsing " + txt + " - error index: " + pos.getErrorIndex());
    } else if (val != num.intValue()) {
        errln("FAIL: Parsed result: " + num + " - expected: " + val);
    }
}
 
Example 3
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 4
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 5
Source File: WriteNumberFormatSerialTestData.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args){
    NumberFormat nf     = NumberFormat.getInstance(Locale.US);
    NumberFormat nfc    = NumberFormat.getCurrencyInstance(Locale.US);
    NumberFormat nfp     = NumberFormat.getPercentInstance(Locale.US);
    NumberFormat nfsp     = NumberFormat.getScientificInstance(Locale.US);
    
    try{
        FileOutputStream file = new FileOutputStream("NumberFormatSerialTestData.java");
        file.write(header.getBytes());
        write(file,(Object)nf,"generalInstance", "//NumberFormat.getInstance(Locale.US)");
        write(file,(Object)nfc,"currencyInstance","//NumberFormat.getCurrencyInstance(Locale.US)");
        write(file,(Object)nfp,"percentInstance","//NumberFormat.getPercentInstance(Locale.US)");
        write(file,(Object)nfsp,"scientificInstance","//NumberFormat.getScientificInstance(Locale.US)");
        file.write(footer.getBytes());            
        file.close();
    }catch( Exception e){
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 6
Source File: GlobalizationPreferences.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * This function can be overridden by subclasses to use different heuristics.
 * <b>It MUST return a 'safe' value,
 * one whose modification will not affect this object.</b>
 *
 * @param style
 * @hide draft / provisional / internal are hidden on Android
 */
protected NumberFormat guessNumberFormat(int style) {
    NumberFormat result;
    ULocale nfLocale = getAvailableLocale(TYPE_NUMBERFORMAT);
    if (nfLocale == null) {
        nfLocale = ULocale.ROOT;
    }
    switch (style) {
    case NF_NUMBER:
        result = NumberFormat.getInstance(nfLocale);
        break;
    case NF_SCIENTIFIC:
        result = NumberFormat.getScientificInstance(nfLocale);
        break;
    case NF_INTEGER:
        result = NumberFormat.getIntegerInstance(nfLocale);
        break;
    case NF_PERCENT:
        result = NumberFormat.getPercentInstance(nfLocale);
        break;
    case NF_CURRENCY:
        result = NumberFormat.getCurrencyInstance(nfLocale);
        result.setCurrency(getCurrency());
        break;
    default:
        throw new IllegalArgumentException("Unknown number format style");
    }
    return result;
}
 
Example 7
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public float assignFloatValue(float returnfloat)
{
    logln(" VALUE " + returnfloat);
    NumberFormat nfcommon =  NumberFormat.getCurrencyInstance(Locale.US);
    nfcommon.setGroupingUsed(false);

    String stringValue = nfcommon.format(returnfloat).substring(1);
    if (Float.valueOf(stringValue).floatValue() != returnfloat)
        errln(" DISPLAYVALUE " + stringValue);
    return returnfloat;
}
 
Example 8
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * NumberFormat.getCurrencyInstance() produces format that uses
 * decimal separator instead of monetary decimal separator.
 *
 * Rewrote this test not to depend on the actual pattern.  Pattern should
 * never contain the monetary separator!  Decimal separator in pattern is
 * interpreted as monetary separator if currency symbol is seen!
 */
@Test
public void Test4087244 () {
    Locale de = new Locale("pt", "PT");
    DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(de);
    DecimalFormatSymbols sym = df.getDecimalFormatSymbols();
    sym.setMonetaryDecimalSeparator('$');
df.setDecimalFormatSymbols(sym);
    char decSep = sym.getDecimalSeparator();
    char monSep = sym.getMonetaryDecimalSeparator();
    //char zero = sym.getZeroDigit(); //The variable is never used
    if (decSep == monSep) {
        errln("ERROR in test: want decimal sep != monetary sep");
    } else {
        df.setMinimumIntegerDigits(1);
        df.setMinimumFractionDigits(2);
        String str = df.format(1.23);
        String monStr = "1" + monSep + "23";
        String decStr = "1" + decSep + "23";
        if (str.indexOf(monStr) >= 0 && str.indexOf(decStr) < 0) {
            logln("OK: 1.23 -> \"" + str + "\" contains \"" +
                  monStr + "\" and not \"" + decStr + '"');
        } else {
            errln("FAIL: 1.23 -> \"" + str + "\", should contain \"" +
                  monStr +
                  "\" and not \"" + decStr + '"');
        }
    }
}
 
Example 9
Source File: NumberFormatRoundTripTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestNumberFormatRoundTrip() {

    NumberFormat fmt = null;

    logln("Default Locale");
    
    logln("Default Number format");
    fmt = NumberFormat.getInstance();
    _test(fmt);

    logln("Currency Format");
    fmt = NumberFormat.getCurrencyInstance();
    _test(fmt);

    logln("Percent Format");
    fmt = NumberFormat.getPercentInstance();
    _test(fmt);


    int locCount = 0;
    final Locale[] loc = NumberFormat.getAvailableLocales();
    if(quick) {
        if(locCount > 5)
            locCount = 5;
        logln("Quick mode: only _testing first 5 Locales");
    }
    for(int i = 0; i < locCount; ++i) {
        logln(loc[i].getDisplayName());

        fmt = NumberFormat.getInstance(loc[i]);
        _test(fmt);
    
        fmt = NumberFormat.getCurrencyInstance(loc[i]);
        _test(fmt);
    
        fmt = NumberFormat.getPercentInstance(loc[i]);
        _test(fmt);
    }

    logln("Numeric error " + min_numeric_error + " to " + max_numeric_error);
}
 
Example 10
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 11
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 12
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 13
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 14
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 + ">");
        }

    }
}