Search in sources :

Example 16 with NumberFormat

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

the class IntlTestDecimalFormatAPIC method TestAPI.

// This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
@Test
public void TestAPI() {
    logln("DecimalFormat API test---");
    logln("");
    Locale.setDefault(Locale.ENGLISH);
    // ======= Test constructors
    logln("Testing DecimalFormat constructors");
    DecimalFormat def = new DecimalFormat();
    final String pattern = new String("#,##0.# FF");
    final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
    final CurrencyPluralInfo infoInput = new CurrencyPluralInfo(ULocale.FRENCH);
    DecimalFormat pat = null;
    try {
        pat = new DecimalFormat(pattern);
    } catch (IllegalArgumentException e) {
        errln("ERROR: Could not create DecimalFormat (pattern)");
    }
    DecimalFormat cust1 = null;
    try {
        cust1 = new DecimalFormat(pattern, symbols);
    } catch (IllegalArgumentException e) {
        errln("ERROR: Could not create DecimalFormat (pattern, symbols)");
    }
    @SuppressWarnings("unused") DecimalFormat cust2 = null;
    try {
        cust2 = new DecimalFormat(pattern, symbols, infoInput, NumberFormat.PLURALCURRENCYSTYLE);
    } catch (IllegalArgumentException e) {
        errln("ERROR: Could not create DecimalFormat (pattern, symbols, infoInput, style)");
    }
    // ======= Test clone(), assignment, and equality
    logln("Testing clone() and equality operators");
    Format clone = (Format) def.clone();
    if (!def.equals(clone)) {
        errln("ERROR: Clone() failed");
    }
    // ======= Test various format() methods
    logln("Testing various format() methods");
    // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
    // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
    // this works!
    final double d = -10456.00370000000000;
    final long l = 100000000;
    logln("" + Double.toString(d) + " is the double value");
    StringBuffer res1 = new StringBuffer();
    StringBuffer res2 = new StringBuffer();
    StringBuffer res3 = new StringBuffer();
    StringBuffer res4 = new StringBuffer();
    FieldPosition pos1 = new FieldPosition(0);
    FieldPosition pos2 = new FieldPosition(0);
    FieldPosition pos3 = new FieldPosition(0);
    FieldPosition pos4 = new FieldPosition(0);
    res1 = def.format(d, res1, pos1);
    logln("" + Double.toString(d) + " formatted to " + res1);
    res2 = pat.format(l, res2, pos2);
    logln("" + l + " formatted to " + res2);
    res3 = cust1.format(d, res3, pos3);
    logln("" + Double.toString(d) + " formatted to " + res3);
    res4 = cust1.format(l, res4, pos4);
    logln("" + l + " formatted to " + res4);
    // ======= Test parse()
    logln("Testing parse()");
    String text = new String("-10,456.0037");
    ParsePosition pos = new ParsePosition(0);
    String patt = new String("#,##0.#");
    pat.applyPattern(patt);
    double d2 = pat.parse(text, pos).doubleValue();
    if (d2 != d) {
        errln("ERROR: Roundtrip failed (via parse(" + Double.toString(d2) + " != " + Double.toString(d) + ")) for " + text);
    }
    logln(text + " parsed into " + (long) d2);
    // ======= Test getters and setters
    logln("Testing getters and setters");
    final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
    def.setDecimalFormatSymbols(syms);
    if (!pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
        errln("ERROR: set DecimalFormatSymbols() failed");
    }
    String posPrefix;
    pat.setPositivePrefix("+");
    posPrefix = pat.getPositivePrefix();
    logln("Positive prefix (should be +): " + posPrefix);
    if (posPrefix != "+") {
        errln("ERROR: setPositivePrefix() failed");
    }
    String negPrefix;
    pat.setNegativePrefix("-");
    negPrefix = pat.getNegativePrefix();
    logln("Negative prefix (should be -): " + negPrefix);
    if (negPrefix != "-") {
        errln("ERROR: setNegativePrefix() failed");
    }
    String posSuffix;
    pat.setPositiveSuffix("_");
    posSuffix = pat.getPositiveSuffix();
    logln("Positive suffix (should be _): " + posSuffix);
    if (posSuffix != "_") {
        errln("ERROR: setPositiveSuffix() failed");
    }
    String negSuffix;
    pat.setNegativeSuffix("~");
    negSuffix = pat.getNegativeSuffix();
    logln("Negative suffix (should be ~): " + negSuffix);
    if (negSuffix != "~") {
        errln("ERROR: setNegativeSuffix() failed");
    }
    long multiplier = 0;
    pat.setMultiplier(8);
    multiplier = pat.getMultiplier();
    logln("Multiplier (should be 8): " + multiplier);
    if (multiplier != 8) {
        errln("ERROR: setMultiplier() failed");
    }
    int groupingSize = 0;
    pat.setGroupingSize(2);
    groupingSize = pat.getGroupingSize();
    logln("Grouping size (should be 2): " + (long) groupingSize);
    if (groupingSize != 2) {
        errln("ERROR: setGroupingSize() failed");
    }
    pat.setDecimalSeparatorAlwaysShown(true);
    boolean tf = pat.isDecimalSeparatorAlwaysShown();
    logln("DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
    if (tf != true) {
        errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
    }
    String funkyPat;
    funkyPat = pat.toPattern();
    logln("Pattern is " + funkyPat);
    String locPat;
    locPat = pat.toLocalizedPattern();
    logln("Localized pattern is " + locPat);
    pat.setCurrencyPluralInfo(infoInput);
    if (!infoInput.equals(pat.getCurrencyPluralInfo())) {
        errln("ERROR: set/get CurrencyPluralInfo() failed");
    }
    pat.setCurrencyPluralInfo(infoInput);
    if (!infoInput.equals(pat.getCurrencyPluralInfo())) {
        errln("ERROR: set/get CurrencyPluralInfo() failed");
    }
    // ======= Test applyPattern()
    logln("Testing applyPattern()");
    String p1 = new String("#,##0.0#;(#,##0.0#)");
    logln("Applying pattern " + p1);
    pat.applyPattern(p1);
    String s2;
    s2 = pat.toPattern();
    logln("Extracted pattern is " + s2);
    if (!s2.equals(p1)) {
        errln("ERROR: toPattern() result did not match pattern applied");
    }
    String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
    logln("Applying pattern " + p2);
    pat.applyLocalizedPattern(p2);
    String s3;
    s3 = pat.toLocalizedPattern();
    logln("Extracted pattern is " + s3);
    if (!s3.equals(p2)) {
        errln("ERROR: toLocalizedPattern() result did not match pattern applied");
    }
// ======= Test getStaticClassID()
// logln("Testing instanceof()");
// try {
// NumberFormat test = new DecimalFormat();
// if (! (test instanceof DecimalFormat)) {
// errln("ERROR: instanceof failed");
// }
// }
// catch (Exception e) {
// errln("ERROR: Couldn't create a DecimalFormat");
// }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) CurrencyPluralInfo(android.icu.text.CurrencyPluralInfo) DecimalFormat(android.icu.text.DecimalFormat) Format(java.text.Format) NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 17 with NumberFormat

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

the class IntlTestNumberFormatAPI method TestAPI.

// This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
@Test
public void TestAPI() {
    logln("NumberFormat API test---");
    logln("");
    Locale.setDefault(Locale.ENGLISH);
    // ======= Test constructors
    logln("Testing NumberFormat constructors");
    NumberFormat def = NumberFormat.getInstance();
    NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH);
    NumberFormat cur = NumberFormat.getCurrencyInstance();
    NumberFormat cur_fr = NumberFormat.getCurrencyInstance(Locale.FRENCH);
    NumberFormat per = NumberFormat.getPercentInstance();
    NumberFormat per_fr = NumberFormat.getPercentInstance(Locale.FRENCH);
    NumberFormat integer = NumberFormat.getIntegerInstance();
    NumberFormat int_fr = NumberFormat.getIntegerInstance(Locale.FRENCH);
    // Fix "The variable is never used" compilation warnings
    logln("Currency : " + cur.format(1234.5));
    logln("Percent : " + per.format(1234.5));
    logln("Integer : " + integer.format(1234.5));
    logln("Int_fr : " + int_fr.format(1234.5));
    // ======= Test equality
    logln("Testing equality operator");
    if (per_fr.equals(cur_fr)) {
        errln("ERROR: == failed");
    }
    // ======= Test various format() methods
    logln("Testing various format() methods");
    // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
    // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
    // this works!
    final double d = -10456.00370000000000;
    final long l = 100000000;
    String res1 = new String();
    String res2 = new String();
    StringBuffer res3 = new StringBuffer();
    StringBuffer res4 = new StringBuffer();
    StringBuffer res5 = new StringBuffer();
    StringBuffer res6 = new StringBuffer();
    FieldPosition pos1 = new FieldPosition(0);
    FieldPosition pos2 = new FieldPosition(0);
    FieldPosition pos3 = new FieldPosition(0);
    FieldPosition pos4 = new FieldPosition(0);
    res1 = cur_fr.format(d);
    logln("" + d + " formatted to " + res1);
    res2 = cur_fr.format(l);
    logln("" + l + " formatted to " + res2);
    res3 = cur_fr.format(d, res3, pos1);
    logln("" + d + " formatted to " + res3);
    res4 = cur_fr.format(l, res4, pos2);
    logln("" + l + " formatted to " + res4);
    res5 = cur_fr.format(d, res5, pos3);
    logln("" + d + " formatted to " + res5);
    res6 = cur_fr.format(l, res6, pos4);
    logln("" + l + " formatted to " + res6);
    // ======= Test parse()
    logln("Testing parse()");
    // String text = new String("-10,456.0037");
    String text = new String("-10456,0037");
    ParsePosition pos = new ParsePosition(0);
    ParsePosition pos01 = new ParsePosition(0);
    double d1 = ((Number) fr.parseObject(text, pos)).doubleValue();
    if (d1 != d) {
        errln("ERROR: Roundtrip failed (via parse()) for " + text);
    }
    logln(text + " parsed into " + d1);
    double d2 = fr.parse(text, pos01).doubleValue();
    if (d2 != d) {
        errln("ERROR: Roundtrip failed (via parse()) for " + text);
    }
    logln(text + " parsed into " + d2);
    double d3 = 0;
    try {
        d3 = fr.parse(text).doubleValue();
    } catch (ParseException e) {
        errln("ERROR: parse() failed");
    }
    if (d3 != d) {
        errln("ERROR: Roundtrip failed (via parse()) for " + text);
    }
    logln(text + " parsed into " + d3);
    // ======= Test getters and setters
    logln("Testing getters and setters");
    final Locale[] locales = NumberFormat.getAvailableLocales();
    long count = locales.length;
    logln("Got " + count + " locales");
    for (int i = 0; i < count; i++) {
        String name;
        name = locales[i].getDisplayName();
        logln(name);
    }
    fr.setParseIntegerOnly(def.isParseIntegerOnly());
    if (fr.isParseIntegerOnly() != def.isParseIntegerOnly()) {
        errln("ERROR: setParseIntegerOnly() failed");
    }
    fr.setGroupingUsed(def.isGroupingUsed());
    if (fr.isGroupingUsed() != def.isGroupingUsed()) {
        errln("ERROR: setGroupingUsed() failed");
    }
    fr.setMaximumIntegerDigits(def.getMaximumIntegerDigits());
    if (fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits()) {
        errln("ERROR: setMaximumIntegerDigits() failed");
    }
    fr.setMinimumIntegerDigits(def.getMinimumIntegerDigits());
    if (fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits()) {
        errln("ERROR: setMinimumIntegerDigits() failed");
    }
    fr.setMaximumFractionDigits(def.getMaximumFractionDigits());
    if (fr.getMaximumFractionDigits() != def.getMaximumFractionDigits()) {
        errln("ERROR: setMaximumFractionDigits() failed");
    }
    fr.setMinimumFractionDigits(def.getMinimumFractionDigits());
    if (fr.getMinimumFractionDigits() != def.getMinimumFractionDigits()) {
        errln("ERROR: setMinimumFractionDigits() failed");
    }
// ======= Test getStaticClassID()
// logln("Testing instanceof()");
// try {
// NumberFormat test = new DecimalFormat();
// if (! (test instanceof DecimalFormat)) {
// errln("ERROR: instanceof failed");
// }
// }
// catch (Exception e) {
// errln("ERROR: Couldn't create a DecimalFormat");
// }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ParseException(java.text.ParseException) FieldPosition(java.text.FieldPosition) NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 18 with NumberFormat

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

the class NumberFormatRegistrationTest method TestRegistration.

@Test
public void TestRegistration() {
    final ULocale SRC_LOC = ULocale.FRANCE;
    final ULocale SWAP_LOC = ULocale.US;
    class TestFactory extends SimpleNumberFormatFactory {

        NumberFormat currencyStyle;

        TestFactory() {
            this(SRC_LOC, SWAP_LOC);
        }

        TestFactory(ULocale srcLoc, ULocale swapLoc) {
            super(srcLoc);
            currencyStyle = NumberFormat.getIntegerInstance(swapLoc);
        }

        public NumberFormat createFormat(ULocale loc, int formatType) {
            if (formatType == FORMAT_CURRENCY) {
                return currencyStyle;
            }
            return null;
        }
    }
    {
        try {
            NumberFormat.unregister(null);
            errln("did not throw exception on null unregister");
        } catch (Exception e) {
            logln("PASS: null unregister failed as expected");
        }
        try {
            NumberFormat.registerFactory(null);
            errln("did not throw exception on null register");
        } catch (Exception e) {
            logln("PASS: null register failed as expected");
        }
        try {
            // an exception.
            if (NumberFormat.unregister("")) {
                errln("unregister of empty string key succeeded");
            }
        } catch (Exception e) {
        }
    }
    ULocale fu_FU = new ULocale("fu_FU");
    NumberFormat f0 = NumberFormat.getIntegerInstance(SWAP_LOC);
    NumberFormat f1 = NumberFormat.getIntegerInstance(SRC_LOC);
    NumberFormat f2 = NumberFormat.getCurrencyInstance(SRC_LOC);
    Object key = NumberFormat.registerFactory(new TestFactory());
    Object key2 = NumberFormat.registerFactory(new TestFactory(fu_FU, ULocale.GERMANY));
    if (!Arrays.asList(NumberFormat.getAvailableULocales()).contains(fu_FU)) {
        errln("did not list fu_FU");
    }
    NumberFormat f3 = NumberFormat.getCurrencyInstance(SRC_LOC);
    NumberFormat f4 = NumberFormat.getIntegerInstance(SRC_LOC);
    // restore for other tests
    NumberFormat.unregister(key);
    NumberFormat f5 = NumberFormat.getCurrencyInstance(SRC_LOC);
    NumberFormat.unregister(key2);
    float n = 1234.567f;
    logln("f0 swap int: " + f0.format(n));
    logln("f1 src int: " + f1.format(n));
    logln("f2 src cur: " + f2.format(n));
    logln("f3 reg cur: " + f3.format(n));
    logln("f4 reg int: " + f4.format(n));
    logln("f5 unreg cur: " + f5.format(n));
    if (!f3.format(n).equals(f0.format(n))) {
        errln("registered service did not match");
    }
    if (!f4.format(n).equals(f1.format(n))) {
        errln("registered service did not inherit");
    }
    if (!f5.format(n).equals(f2.format(n))) {
        errln("unregistered service did not match original");
    }
    // coverage
    NumberFormat f6 = NumberFormat.getNumberInstance(fu_FU);
    if (f6 == null) {
        errln("getNumberInstance(fu_FU) returned null");
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleNumberFormatFactory(android.icu.text.NumberFormat.SimpleNumberFormatFactory) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 19 with NumberFormat

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

the class NumberFormatSpecificationTest method TestNfSetters.

@Test
public void TestNfSetters() {
    NumberFormat nf = nfWithPattern("#,##0.##");
    nf.setMaximumIntegerDigits(5);
    nf.setMinimumIntegerDigits(4);
    assertEquals("", "34 567,89", format(1234567.89, nf));
    assertEquals("", "0 034,56", format(34.56, nf));
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 20 with NumberFormat

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

the class GlobalizationPreferences method guessNumberFormat.

/**
 * 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;
}
Also used : NumberFormat(android.icu.text.NumberFormat)

Aggregations

NumberFormat (android.icu.text.NumberFormat)105 Test (org.junit.Test)84 ULocale (android.icu.util.ULocale)39 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)35 DecimalFormat (android.icu.text.DecimalFormat)27 Locale (java.util.Locale)24 ParseException (java.text.ParseException)20 ParsePosition (java.text.ParsePosition)13 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)12 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)11 FieldPosition (java.text.FieldPosition)9 IOException (java.io.IOException)7 DateFormat (android.icu.text.DateFormat)5 Calendar (android.icu.util.Calendar)5 Date (java.util.Date)5 BigDecimal (android.icu.math.BigDecimal)4 DisplayContext (android.icu.text.DisplayContext)4 MeasureFormat (android.icu.text.MeasureFormat)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 PluralFormat (android.icu.text.PluralFormat)3