Search in sources :

Example 51 with DecimalFormat

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

the class NumberFormatTest method TestFieldPositionDecimal.

@Test
public void TestFieldPositionDecimal() {
    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.DECIMAL_SEPARATOR);
    nf.format(35.47, buffer, fp);
    assertEquals("35.47", "FOO35.47BA", buffer.toString());
    assertEquals("fp begin", 5, fp.getBeginIndex());
    assertEquals("fp end", 6, fp.getEndIndex());
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 52 with DecimalFormat

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

the class NumberFormatTest method TestMissingFieldPositionsNegativeBigDec.

@Test
public void TestMissingFieldPositionsNegativeBigDec() {
    // Check complex positive;negative pattern.
    DecimalFormatSymbols us_symbols = new DecimalFormatSymbols(ULocale.US);
    DecimalFormat fmtPosNegSign = new DecimalFormat("+0.####E+00;-0.#######E+0", us_symbols);
    Number negativeExp = new BigDecimal("-0.000000987654321083");
    String negExpFormatted = fmtPosNegSign.format(negativeExp);
    checkFormatWithField("sign", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.SIGN, 0, 1);
    checkFormatWithField("integer", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.INTEGER, 1, 2);
    checkFormatWithField("decimal separator", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.DECIMAL_SEPARATOR, 2, 3);
    checkFormatWithField("fraction", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.FRACTION, 3, 7);
    checkFormatWithField("exponent symbol", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.EXPONENT_SYMBOL, 7, 8);
    checkFormatWithField("exponent sign", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.EXPONENT_SIGN, 8, 9);
    checkFormatWithField("exponent", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.EXPONENT, 9, 11);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 53 with DecimalFormat

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

the class NumberFormatTest method TestIllegalPatterns.

@Test
public void TestIllegalPatterns() {
    // Test cases:
    // Prefix with "-:" for illegal patterns
    // Prefix with "+:" for legal patterns
    String[] DATA = { // Unquoted special characters in the suffix are illegal
    "-:000.000|###", "+:000.000'|###'" };
    for (int i = 0; i < DATA.length; ++i) {
        String pat = DATA[i];
        boolean valid = pat.charAt(0) == '+';
        pat = pat.substring(2);
        Exception e = null;
        try {
            // locale doesn't matter here
            new DecimalFormat(pat);
        } catch (IllegalArgumentException e1) {
            e = e1;
        } catch (IndexOutOfBoundsException e1) {
            e = e1;
        }
        String msg = (e == null) ? "success" : e.getMessage();
        if ((e == null) == valid) {
            logln("Ok: pattern \"" + pat + "\": " + msg);
        } else {
            errln("FAIL: pattern \"" + pat + "\" should have " + (valid ? "succeeded" : "failed") + "; got " + msg);
        }
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 54 with DecimalFormat

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

the class NumberFormatTest method TestScientific2.

@Test
public void TestScientific2() {
    // jb 2552
    DecimalFormat fmt = (DecimalFormat) NumberFormat.getCurrencyInstance();
    Number num = new Double(12.34);
    expect(fmt, num, "$12.34");
    fmt.setScientificNotation(true);
    expect(fmt, num, "$1.23E1");
    fmt.setScientificNotation(false);
    expect(fmt, num, "$12.34");
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 55 with DecimalFormat

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

the class NumberFormatTest method TestSetCurrency.

@Test
public void TestSetCurrency() {
    DecimalFormatSymbols decf1 = DecimalFormatSymbols.getInstance(ULocale.US);
    DecimalFormatSymbols decf2 = DecimalFormatSymbols.getInstance(ULocale.US);
    decf2.setCurrencySymbol("UKD");
    DecimalFormat format1 = new DecimalFormat("000.000", decf1);
    DecimalFormat format2 = new DecimalFormat("000.000", decf2);
    Currency euro = Currency.getInstance("EUR");
    format1.setCurrency(euro);
    format2.setCurrency(euro);
    assertEquals("Reset with currency symbol", format1, format2);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Currency(android.icu.util.Currency) 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