Search in sources :

Example 61 with DecimalFormat

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

the class MyNumberFormat method Test4243108.

/**
 * 4243108: format(0.0) gives "0.1" if preceded by parse("99.99")
 */
@Test
public void Test4243108() {
    DecimalFormat f = new DecimalFormat("#.#");
    String result = f.format(0.0);
    if (result.equals("0")) {
        logln("OK: got " + result);
    } else {
        errln("FAIL: got " + result);
    }
    try {
        double dResult = f.parse("99.99").doubleValue();
        if (dResult == 99.99) {
            logln("OK: got " + dResult);
        } else {
            errln("FAIL: got " + dResult);
        }
    } catch (ParseException e) {
        errln("Caught a ParseException:");
        e.printStackTrace();
    }
    result = f.format(0.0);
    if (result.equals("0")) {
        logln("OK: got " + result);
    } else {
        errln("FAIL: got " + result);
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 62 with DecimalFormat

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

the class MyNumberFormat method Test4087244.

/**
 * NumberFormat.getCurrencyInstance() produces format that uses
 * decimal separator instead of monetary decimal separator.
 *
 * Rewrote this test not to depend on the actual pattern.  Pattern should
 * never contain the monetary separator!  Decimal separator in pattern is
 * interpreted as monetary separator if currency symbol is seen!
 */
@Test
public void Test4087244() {
    Locale de = new Locale("pt", "PT");
    DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(de);
    DecimalFormatSymbols sym = df.getDecimalFormatSymbols();
    sym.setMonetaryDecimalSeparator('$');
    df.setDecimalFormatSymbols(sym);
    char decSep = sym.getDecimalSeparator();
    char monSep = sym.getMonetaryDecimalSeparator();
    // char zero = sym.getZeroDigit(); //The variable is never used
    if (decSep == monSep) {
        errln("ERROR in test: want decimal sep != monetary sep");
    } else {
        df.setMinimumIntegerDigits(1);
        df.setMinimumFractionDigits(2);
        String str = df.format(1.23);
        String monStr = "1" + monSep + "23";
        String decStr = "1" + decSep + "23";
        if (str.indexOf(monStr) >= 0 && str.indexOf(decStr) < 0) {
            logln("OK: 1.23 -> \"" + str + "\" contains \"" + monStr + "\" and not \"" + decStr + '"');
        } else {
            errln("FAIL: 1.23 -> \"" + str + "\", should contain \"" + monStr + "\" and not \"" + decStr + '"');
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 63 with DecimalFormat

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

the class MyNumberFormat method Test4062486.

/**
 * API tests for API addition request A23. FieldPosition.getBeginIndex and
 * FieldPosition.getEndIndex.
 */
@Test
public void Test4062486() {
    DecimalFormat fmt = new DecimalFormat("#,##0.00");
    StringBuffer formatted = new StringBuffer();
    FieldPosition field = new FieldPosition(0);
    Double num = new Double(1234.5);
    fmt.format(num, formatted, field);
    if (field.getBeginIndex() != 0 && field.getEndIndex() != 5)
        errln("Format 1234.5 failed. Begin index: " + field.getBeginIndex() + " End index: " + field.getEndIndex());
    field.setBeginIndex(7);
    field.setEndIndex(4);
    if (field.getBeginIndex() != 7 && field.getEndIndex() != 4)
        errln("Set begin/end field indexes failed. Begin index: " + field.getBeginIndex() + " End index: " + field.getEndIndex());
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 64 with DecimalFormat

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

the class MyNumberFormat method Test4068693.

/**
 * DecimalFormat.parse returns wrong value
 */
@Test
public void Test4068693() {
    logln("----- Test Application -----");
    // ParsePosition pos;
    DecimalFormat df = new DecimalFormat();
    Number d = df.parse("123.55456", new ParsePosition(0));
    if (!d.toString().equals("123.55456")) {
        errln("Result -> " + d.doubleValue());
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 65 with DecimalFormat

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

the class MyNumberFormat method Test4147295.

/**
 * DecimalFormat.applyPattern() sets minimum integer digits incorrectly.
 * CANNOT REPRODUCE
 * This bug is a duplicate of 4139344, which is a duplicate of 4134300
 */
@Test
public void Test4147295() {
    DecimalFormat sdf = new DecimalFormat();
    String pattern = "#,###";
    logln("Applying pattern \"" + pattern + "\"");
    sdf.applyPattern(pattern);
    int minIntDig = sdf.getMinimumIntegerDigits();
    if (minIntDig != 0) {
        errln("Test failed");
        errln(" Minimum integer digits : " + minIntDig);
        errln(" new pattern: " + sdf.toPattern());
    } else {
        logln("Test passed");
        logln(" Minimum integer digits : " + minIntDig);
    }
}
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