Search in sources :

Example 41 with DecimalFormat

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

the class MyNumberFormat method Test4099404.

/**
 * Tests all different comments.
 * Response to some comments :
 * [1] DecimalFormat.parse API documentation is more than just one line.
 * This is not a reproducable doc error in 116 source code.
 * [2] See updated javadoc.
 * [3] Fixed.
 * [4] NumberFormat.parse(String, ParsePosition) : If parsing fails,
 * a null object will be returned.  The unchanged parse position also
 * reflects an error.
 * NumberFormat.parse(String) : If parsing fails, an ParseException
 * will be thrown.
 * See updated javadoc for more details.
 * [5] See updated javadoc.
 * [6] See updated javadoc.
 * [7] This is a correct behavior if the DateFormat object is linient.
 * Otherwise, an IllegalArgumentException will be thrown when formatting
 * "January 35".  See GregorianCalendar class javadoc for more details.
 */
@Test
public void Test4099404() {
    try {
        DecimalFormat fmt = new DecimalFormat("000.0#0");
        // fix "The variable 'fmt' is never used"
        logln("format 3456.78: " + fmt.format(3456.78));
        errln("Bug 4099404 failed applying illegal pattern \"000.0#0\"");
    } catch (Exception foo) {
        logln("Bug 4099404 pattern \"000.0#0\" passed");
    }
    try {
        DecimalFormat fmt = new DecimalFormat("0#0.000");
        // fix "The variable 'fmt' is never used"
        logln("format 3456.78: " + fmt.format(3456.78));
        errln("Bug 4099404 failed applying illegal pattern \"0#0.000\"");
    } catch (Exception foo) {
        logln("Bug 4099404 pattern \"0#0.000\" passed");
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) InvalidObjectException(java.io.InvalidObjectException) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 42 with DecimalFormat

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

the class NumberFormatTest method TestWhiteSpaceParsing.

@Test
public void TestWhiteSpaceParsing() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt = new DecimalFormat("a  b#0c  ", US);
    int n = 1234;
    expect(fmt, "a b1234c ", n);
    expect(fmt, "a   b1234c   ", n);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 43 with DecimalFormat

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

the class NumberFormatTest method TestFieldPositionFraction.

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

Example 44 with DecimalFormat

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

the class NumberFormatTest method TestNegZeroRounding.

/*
     * Testing rounding to negative zero problem
     * reported by ticket#7609
     */
@Test
public void TestNegZeroRounding() {
    DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
    df.setRoundingMode(MathContext.ROUND_HALF_UP);
    df.setMinimumFractionDigits(1);
    df.setMaximumFractionDigits(1);
    String text1 = df.format(-0.01);
    df.setRoundingIncrement(0.1);
    String text2 = df.format(-0.01);
    // output1 and output2 must be identical
    if (!text1.equals(text2)) {
        errln("NumberFormat.format() should return the same result - text1=" + text1 + " text2=" + text2);
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 45 with DecimalFormat

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

the class NumberFormatTest method TestFormatToCharacterIteratorIssue11805.

// Testing for Issue 11805.
@Test
public void TestFormatToCharacterIteratorIssue11805() {
    final double number = -350.76;
    DecimalFormat dfUS = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.US);
    String strUS = dfUS.format(number);
    Set<AttributedCharacterIterator.Attribute> resultUS = dfUS.formatToCharacterIterator(number).getAllAttributeKeys();
    assertEquals("Negative US Results: " + strUS, 5, resultUS.size());
    // For each test, add assert that all the fields are present and in the right spot.
    // TODO: Add tests for identify and position of each field, as in IntlTestDecimalFormatAPIC.
    DecimalFormat dfDE = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.GERMANY);
    String strDE = dfDE.format(number);
    Set<AttributedCharacterIterator.Attribute> resultDE = dfDE.formatToCharacterIterator(number).getAllAttributeKeys();
    assertEquals("Negative DE Results: " + strDE, 5, resultDE.size());
    DecimalFormat dfIN = (DecimalFormat) DecimalFormat.getCurrencyInstance(new Locale("hi", "in"));
    String strIN = dfIN.format(number);
    Set<AttributedCharacterIterator.Attribute> resultIN = dfIN.formatToCharacterIterator(number).getAllAttributeKeys();
    assertEquals("Negative IN Results: " + strIN, 5, resultIN.size());
    DecimalFormat dfJP = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.JAPAN);
    String strJP = dfJP.format(number);
    Set<AttributedCharacterIterator.Attribute> resultJP = dfJP.formatToCharacterIterator(number).getAllAttributeKeys();
    assertEquals("Negative JA Results: " + strJP, 3, resultJP.size());
    DecimalFormat dfGB = (DecimalFormat) DecimalFormat.getCurrencyInstance(new Locale("en", "gb"));
    String strGB = dfGB.format(number);
    Set<AttributedCharacterIterator.Attribute> resultGB = dfGB.formatToCharacterIterator(number).getAllAttributeKeys();
    assertEquals("Negative GB Results: " + strGB, 5, resultGB.size());
    DecimalFormat dfPlural = (DecimalFormat) NumberFormat.getInstance(new Locale("en", "gb"), NumberFormat.PLURALCURRENCYSTYLE);
    strGB = dfPlural.format(number);
    resultGB = dfPlural.formatToCharacterIterator(number).getAllAttributeKeys();
    assertEquals("Negative GB Results: " + strGB, 5, resultGB.size());
    strGB = dfPlural.format(1);
    resultGB = dfPlural.formatToCharacterIterator(1).getAllAttributeKeys();
    assertEquals("Negative GB Results: " + strGB, 4, resultGB.size());
    // Test output with unit value.
    DecimalFormat auPlural = (DecimalFormat) NumberFormat.getInstance(new Locale("en", "au"), NumberFormat.PLURALCURRENCYSTYLE);
    String strAU = auPlural.format(1L);
    Set<AttributedCharacterIterator.Attribute> resultAU = auPlural.formatToCharacterIterator(1L).getAllAttributeKeys();
    assertEquals("Unit AU Result: " + strAU, 4, resultAU.size());
    // Verify Permille fields.
    DecimalFormatSymbols sym = new DecimalFormatSymbols(new Locale("en", "gb"));
    DecimalFormat dfPermille = new DecimalFormat("####0.##\u2030", sym);
    strGB = dfPermille.format(number);
    resultGB = dfPermille.formatToCharacterIterator(number).getAllAttributeKeys();
    assertEquals("Negative GB Permille Results: " + strGB, 3, resultGB.size());
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) 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