Search in sources :

Example 36 with DecimalFormat

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

the class MyNumberFormat method Test4087535.

/**
 * DecimalFormat.format() incorrectly formats 0.0
 */
@Test
public void Test4087535() {
    DecimalFormat df = new DecimalFormat();
    df.setMinimumIntegerDigits(0);
    double n = 0;
    String buffer = new String();
    buffer = df.format(n);
    if (buffer.length() == 0)
        errln(n + ": '" + buffer + "'");
    n = 0.1;
    buffer = df.format(n);
    if (buffer.length() == 0)
        errln(n + ": '" + buffer + "'");
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 37 with DecimalFormat

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

the class MyNumberFormat method Test4086575.

/**
 * A space as a group separator for localized pattern causes
 * wrong format.  WorkAround : use non-breaking space.
 */
@Test
public void Test4086575() {
    NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
    logln("nf toPattern1: " + ((DecimalFormat) nf).toPattern());
    logln("nf toLocPattern1: " + ((DecimalFormat) nf).toLocalizedPattern());
    // No group separator
    logln("...applyLocalizedPattern ###,00;(###,00) ");
    ((DecimalFormat) nf).applyLocalizedPattern("###,00;(###,00)");
    logln("nf toPattern2: " + ((DecimalFormat) nf).toPattern());
    logln("nf toLocPattern2: " + ((DecimalFormat) nf).toLocalizedPattern());
    // 1234,00
    logln("nf: " + nf.format(1234));
    // (1234,00)
    logln("nf: " + nf.format(-1234));
    // Space as group separator
    logln("...applyLocalizedPattern # ###,00;(# ###,00) ");
    ((DecimalFormat) nf).applyLocalizedPattern("#\u00a0###,00;(#\u00a0###,00)");
    logln("nf toPattern2: " + ((DecimalFormat) nf).toPattern());
    logln("nf toLocPattern2: " + ((DecimalFormat) nf).toLocalizedPattern());
    String buffer = nf.format(1234);
    if (!buffer.equals("1\u00a0234,00"))
        // Expect 1 234,00
        errln("nf : " + buffer);
    buffer = nf.format(-1234);
    if (!buffer.equals("(1\u00a0234,00)"))
        // Expect (1 234,00)
        errln("nf : " + buffer);
// Erroneously prints:
// 1234,00 ,
// (1234,00 ,)
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 38 with DecimalFormat

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

the class MyNumberFormat method Test4087245.

/**
 * DecimalFormatSymbols should be cloned in the ctor DecimalFormat.
 * DecimalFormat(String, DecimalFormatSymbols).
 */
@Test
public void Test4087245() {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    DecimalFormat df = new DecimalFormat("#,##0.0", symbols);
    long n = 123;
    StringBuffer buf1 = new StringBuffer();
    StringBuffer buf2 = new StringBuffer();
    logln("format(" + n + ") = " + df.format(n, buf1, new FieldPosition(0)));
    // change value of field
    symbols.setDecimalSeparator('p');
    logln("format(" + n + ") = " + df.format(n, buf2, new FieldPosition(0)));
    if (!buf1.toString().equals(buf2.toString()))
        errln("Test for bug 4087245 failed");
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 39 with DecimalFormat

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

the class MyNumberFormat method Test4108738.

/**
 * DecimalFormat.parse incorrectly works with a group separator.
 */
@Test
public void Test4108738() {
    DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(java.util.Locale.US));
    String text = "1.222,111";
    Number num = df.parse(text, new ParsePosition(0));
    if (!num.toString().equals("1.222"))
        errln("\"" + text + "\"  is parsed as " + num);
    text = "1.222x111";
    num = df.parse(text, new ParsePosition(0));
    if (!num.toString().equals("1.222"))
        errln("\"" + text + "\"  is parsed as " + num);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 40 with DecimalFormat

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

the class MyNumberFormat method Test4170798.

/**
 * DecimalFormat.parse() fails when ParseIntegerOnly set to true
 */
@Test
public void Test4170798() {
    Locale savedLocale = Locale.getDefault();
    Locale.setDefault(Locale.US);
    DecimalFormat df = new DecimalFormat();
    df.setParseIntegerOnly(true);
    Number n = df.parse("-0.0", new ParsePosition(0));
    if (!(n instanceof Double) || n.intValue() != 0) {
        errln("FAIL: parse(\"-0.0\") returns " + n + " (" + n.getClass().getName() + ')');
    }
    Locale.setDefault(savedLocale);
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormat(android.icu.text.DecimalFormat) ParsePosition(java.text.ParsePosition) 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