Search in sources :

Example 56 with DecimalFormat

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

the class NumberFormatTest method TestFieldPositionFractionButInteger.

@Test
public void TestFieldPositionFractionButInteger() {
    DecimalFormat nf = (DecimalFormat) android.icu.text.NumberFormat.getInstance(ULocale.ENGLISH);
    nf.setPositivePrefix("FOO");
    nf.setPositiveSuffix("BA");
    StringBuffer buffer = new StringBuffer();
    FieldPosition fp = new FieldPosition(NumberFormat.Field.FRACTION);
    nf.format(35, buffer, fp);
    assertEquals("35", "FOO35BA", buffer.toString());
    assertEquals("fp begin", 5, fp.getBeginIndex());
    assertEquals("fp end", 5, fp.getEndIndex());
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 57 with DecimalFormat

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

the class NumberFormatTest method TestCoverage.

@Test
public void TestCoverage() {
    // default locale
    NumberFormat fmt = NumberFormat.getNumberInstance();
    logln(fmt.format(new BigInteger("1234567890987654321234567890987654321", 10)));
    // default locale
    fmt = NumberFormat.getScientificInstance();
    logln(fmt.format(PI.INSTANCE));
    try {
        logln(fmt.format("12345"));
        errln("numberformat of string did not throw exception");
    } catch (Exception e) {
        logln("PASS: numberformat of string failed as expected");
    }
    int hash = fmt.hashCode();
    logln("hash code " + hash);
    logln("compare to string returns: " + fmt.equals(""));
    // For ICU 2.6 - alan
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat df = new DecimalFormat("'*&'' '\u00A4' ''&*' #,##0.00", US);
    df.setCurrency(Currency.getInstance("INR"));
    expect2(df, 1.0, "*&' \u20B9 '&* 1.00");
    expect2(df, -2.0, "-*&' \u20B9 '&* 2.00");
    df.applyPattern("#,##0.00 '*&'' '\u00A4' ''&*'");
    expect2(df, 2.0, "2.00 *&' \u20B9 '&*");
    expect2(df, -1.0, "-1.00 *&' \u20B9 '&*");
    java.math.BigDecimal r;
    r = df.getRoundingIncrement();
    if (r != null) {
        errln("FAIL: rounding = " + r + ", expect null");
    }
    if (df.isScientificNotation()) {
        errln("FAIL: isScientificNotation = true, expect false");
    }
    df.applyPattern("0.00000");
    df.setScientificNotation(true);
    if (!df.isScientificNotation()) {
        errln("FAIL: isScientificNotation = false, expect true");
    }
    df.setMinimumExponentDigits((byte) 2);
    if (df.getMinimumExponentDigits() != 2) {
        errln("FAIL: getMinimumExponentDigits = " + df.getMinimumExponentDigits() + ", expect 2");
    }
    df.setExponentSignAlwaysShown(true);
    if (!df.isExponentSignAlwaysShown()) {
        errln("FAIL: isExponentSignAlwaysShown = false, expect true");
    }
    df.setSecondaryGroupingSize(0);
    if (df.getSecondaryGroupingSize() != 0) {
        errln("FAIL: getSecondaryGroupingSize = " + df.getSecondaryGroupingSize() + ", expect 0");
    }
    expect2(df, 3.14159, "3.14159E+00");
    // DecimalFormatSymbols#getInstance
    DecimalFormatSymbols decsym1 = DecimalFormatSymbols.getInstance();
    DecimalFormatSymbols decsym2 = new DecimalFormatSymbols();
    if (!decsym1.equals(decsym2)) {
        errln("FAIL: DecimalFormatSymbols returned by getInstance()" + "does not match new DecimalFormatSymbols().");
    }
    decsym1 = DecimalFormatSymbols.getInstance(Locale.JAPAN);
    decsym2 = DecimalFormatSymbols.getInstance(ULocale.JAPAN);
    if (!decsym1.equals(decsym2)) {
        errln("FAIL: DecimalFormatSymbols returned by getInstance(Locale.JAPAN)" + "does not match the one returned by getInstance(ULocale.JAPAN).");
    }
    // DecimalFormatSymbols#getAvailableLocales/#getAvailableULocales
    Locale[] allLocales = DecimalFormatSymbols.getAvailableLocales();
    if (allLocales.length == 0) {
        errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableLocales");
    } else {
        logln("PASS: " + allLocales.length + " available locales returned by DecimalFormatSymbols.getAvailableLocales");
    }
    ULocale[] allULocales = DecimalFormatSymbols.getAvailableULocales();
    if (allULocales.length == 0) {
        errln("FAIL: Got a empty list for DecimalFormatSymbols.getAvailableLocales");
    } else {
        logln("PASS: " + allULocales.length + " available locales returned by DecimalFormatSymbols.getAvailableULocales");
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigInteger(java.math.BigInteger) ParseException(java.text.ParseException) IOException(java.io.IOException) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 58 with DecimalFormat

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

the class NumberFormatTest method TestParseNegativeWithFaLocale.

@Test
public void TestParseNegativeWithFaLocale() {
    DecimalFormat parser = (DecimalFormat) NumberFormat.getInstance(new ULocale("fa"));
    try {
        double value = parser.parse("-0,5").doubleValue();
        assertEquals("Expect -0.5", -0.5, value);
    } catch (ParseException e) {
        TestFmwk.errln("Parsing -0.5 should have succeeded.");
    }
}
Also used : ULocale(android.icu.util.ULocale) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 59 with DecimalFormat

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

the class NumberFormatTest method TestJB5358.

@Test
public void TestJB5358() {
    int numThreads = 10;
    String numstr = "12345";
    double expected = 12345;
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt = new DecimalFormat("#.#", sym);
    ArrayList errors = new ArrayList();
    ParseThreadJB5358[] threads = new ParseThreadJB5358[numThreads];
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new ParseThreadJB5358((DecimalFormat) fmt.clone(), numstr, expected, errors);
        threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }
    if (errors.size() != 0) {
        StringBuffer errBuf = new StringBuffer();
        for (int i = 0; i < errors.size(); i++) {
            errBuf.append((String) errors.get(i));
            errBuf.append("\n");
        }
        errln("FAIL: " + errBuf);
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 60 with DecimalFormat

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

the class NumberFormatTest method TestMissingFieldPositionsCurrency.

@Test
public void TestMissingFieldPositionsCurrency() {
    DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(ULocale.US);
    Number number = new Double(92314587.66);
    String result = "$92,314,587.66";
    checkFormatWithField("currency", formatter, number, result, NumberFormat.Field.CURRENCY, 0, 1);
    checkFormatWithField("integer", formatter, number, result, NumberFormat.Field.INTEGER, 1, 11);
    checkFormatWithField("grouping separator", formatter, number, result, NumberFormat.Field.GROUPING_SEPARATOR, 3, 4);
    checkFormatWithField("decimal separator", formatter, number, result, NumberFormat.Field.DECIMAL_SEPARATOR, 11, 12);
    checkFormatWithField("fraction", formatter, number, result, NumberFormat.Field.FRACTION, 12, 14);
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Aggregations

DecimalFormat (android.icu.text.DecimalFormat)150 Test (org.junit.Test)138 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)70 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)57 NumberFormat (android.icu.text.NumberFormat)30 ULocale (android.icu.util.ULocale)28 FieldPosition (java.text.FieldPosition)25 ParseException (java.text.ParseException)23 Locale (java.util.Locale)18 ParsePosition (java.text.ParsePosition)15 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)12 BigDecimal (android.icu.math.BigDecimal)10 IOException (java.io.IOException)8 InvalidObjectException (java.io.InvalidObjectException)5 BigInteger (java.math.BigInteger)5 ScientificNumberFormatter (android.icu.text.ScientificNumberFormatter)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 Format (java.text.Format)4 DateFormat (android.icu.text.DateFormat)3 MessageFormat (android.icu.text.MessageFormat)3