Search in sources :

Example 31 with DecimalFormat

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

the class NumberFormatTest method TestRoundUnnecessarytIssue11808.

// Testing for Issue 11808.
@Test
public void TestRoundUnnecessarytIssue11808() {
    DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
    StringBuffer result = new StringBuffer("");
    df.setRoundingMode(BigDecimal.ROUND_UNNECESSARY);
    df.applyPattern("00.0#E0");
    try {
        df.format(99999.0, result, new FieldPosition(0));
        fail("Missing ArithmeticException for double: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(99999, result, new FieldPosition(0));
        fail("Missing ArithmeticException for int: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(new BigInteger("999999"), result, new FieldPosition(0));
        fail("Missing ArithmeticException for BigInteger: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(new BigDecimal("99999"), result, new FieldPosition(0));
        fail("Missing ArithmeticException for BigDecimal: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(new BigDecimal("-99999"), result, new FieldPosition(0));
        fail("Missing ArithmeticException for BigDecimal: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigInteger(java.math.BigInteger) FieldPosition(java.text.FieldPosition) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 32 with DecimalFormat

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

the class NumberFormatTest method TestMissingFieldPositionsPositiveBigDec.

@Test
public void TestMissingFieldPositionsPositiveBigDec() {
    // 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 positiveExp = new Double("9876543210");
    String posExpFormatted = fmtPosNegSign.format(positiveExp);
    checkFormatWithField("sign", fmtPosNegSign, positiveExp, posExpFormatted, NumberFormat.Field.SIGN, 0, 1);
    checkFormatWithField("integer", fmtPosNegSign, positiveExp, posExpFormatted, NumberFormat.Field.INTEGER, 1, 2);
    checkFormatWithField("decimal separator", fmtPosNegSign, positiveExp, posExpFormatted, NumberFormat.Field.DECIMAL_SEPARATOR, 2, 3);
    checkFormatWithField("fraction", fmtPosNegSign, positiveExp, posExpFormatted, NumberFormat.Field.FRACTION, 3, 7);
    checkFormatWithField("exponent symbol", fmtPosNegSign, positiveExp, posExpFormatted, NumberFormat.Field.EXPONENT_SYMBOL, 7, 8);
    checkFormatWithField("exponent sign", fmtPosNegSign, positiveExp, posExpFormatted, NumberFormat.Field.EXPONENT_SIGN, 8, 9);
    checkFormatWithField("exponent", fmtPosNegSign, positiveExp, posExpFormatted, NumberFormat.Field.EXPONENT, 9, 11);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 33 with DecimalFormat

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

the class MyNumberFormat method Test4134034.

/**
 **
 * DecimalFormat produces extra zeros when formatting numbers.
 */
@Test
public void Test4134034() {
    DecimalFormat nf = new DecimalFormat("##,###,###.00");
    String f = nf.format(9.02);
    if (f.equals("9.02"))
        logln(f + " ok");
    else
        errln("9.02 -> " + f + "; want 9.02");
    f = nf.format(0);
    if (f.equals(".00"))
        logln(f + " ok");
    else
        errln("0 -> " + f + "; want .00");
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 34 with DecimalFormat

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

the class MyNumberFormat method Test4092480.

/**
 * DecimalFormat: Negative format ignored.
 */
@Test
public void Test4092480() {
    DecimalFormat dfFoo = new DecimalFormat("000");
    try {
        dfFoo.applyPattern("0000;-000");
        if (!dfFoo.toPattern().equals("#0000"))
            errln("dfFoo.toPattern : " + dfFoo.toPattern());
        logln(dfFoo.format(42));
        logln(dfFoo.format(-42));
        dfFoo.applyPattern("000;-000");
        if (!dfFoo.toPattern().equals("#000"))
            errln("dfFoo.toPattern : " + dfFoo.toPattern());
        logln(dfFoo.format(42));
        logln(dfFoo.format(-42));
        dfFoo.applyPattern("000;-0000");
        if (!dfFoo.toPattern().equals("#000"))
            errln("dfFoo.toPattern : " + dfFoo.toPattern());
        logln(dfFoo.format(42));
        logln(dfFoo.format(-42));
        dfFoo.applyPattern("0000;-000");
        if (!dfFoo.toPattern().equals("#0000"))
            errln("dfFoo.toPattern : " + dfFoo.toPattern());
        logln(dfFoo.format(42));
        logln(dfFoo.format(-42));
    } catch (Exception foo) {
        errln("Message " + foo.getMessage());
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) InvalidObjectException(java.io.InvalidObjectException) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 35 with DecimalFormat

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

the class MyNumberFormat method Test4087251.

/**
 * DecimalFormat.applyPattern(String) allows illegal patterns
 */
@Test
public void Test4087251() {
    DecimalFormat df = new DecimalFormat();
    try {
        df.applyPattern("#.#.#");
        logln("toPattern() returns \"" + df.toPattern() + "\"");
        errln("applyPattern(\"#.#.#\") doesn't throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        logln("Caught Illegal Argument Error !");
    }
    // Second test; added 5/11/98 when reported to fail on 1.2b3
    try {
        df.applyPattern("#0.0#0#0");
        logln("toPattern() returns \"" + df.toPattern() + "\"");
        errln("applyPattern(\"#0.0#0#0\") doesn't throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        logln("Ok - IllegalArgumentException for #0.0#0#0");
    }
}
Also used : 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