Search in sources :

Example 71 with DecimalFormat

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

the class MyNumberFormat method Test4125885.

/**
 * DecimalFormat.format() delivers wrong string.
 */
@Test
public void Test4125885() {
    double rate = 12.34;
    DecimalFormat formatDec = new DecimalFormat("000.00");
    logln("toPattern: " + formatDec.toPattern());
    String rateString = formatDec.format(rate);
    if (!rateString.equals("012.34"))
        errln("result : " + rateString + " expected : 012.34");
    rate = 0.1234;
    formatDec = null;
    formatDec = new DecimalFormat("+000.00%;-000.00%");
    logln("toPattern: " + formatDec.toPattern());
    rateString = formatDec.format(rate);
    if (!rateString.equals("+012.34%"))
        errln("result : " + rateString + " expected : +012.34%");
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 72 with DecimalFormat

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

the class MyNumberFormat method Test4106658.

/**
 * DecimalFormat.format() incorrectly formats negative doubles.
 */
@Test
public void Test4106658() {
    Locale savedLocale = Locale.getDefault();
    Locale.setDefault(Locale.US);
    // Corrected; see 4147706
    DecimalFormat df = new DecimalFormat();
    double d1 = -0.0;
    double d2 = -0.0001;
    StringBuffer buffer = new StringBuffer();
    logln("pattern: \"" + df.toPattern() + "\"");
    df.format(d1, buffer, new FieldPosition(0));
    if (!buffer.toString().equals("-0")) {
        // Corrected; see 4147706
        errln(d1 + "      is formatted as " + buffer);
    }
    buffer.setLength(0);
    df.format(d2, buffer, new FieldPosition(0));
    if (!buffer.toString().equals("-0")) {
        // Corrected; see 4147706
        errln(d2 + "      is formatted as " + buffer);
    }
    Locale.setDefault(savedLocale);
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 73 with DecimalFormat

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

the class MyNumberFormat method Test4093610.

/* bug 4071859
     * Test rounding for nearest even.
     */
@Test
public void Test4093610() {
    DecimalFormat df = new DecimalFormat("#0.#");
    roundingTest(df, 12.35, "12.4");
    roundingTest(df, 12.45, "12.4");
    roundingTest(df, 12.452, "12.5");
    roundingTest(df, 12.55, "12.6");
    roundingTest(df, 12.65, "12.6");
    roundingTest(df, 12.652, "12.7");
    roundingTest(df, 12.75, "12.8");
    roundingTest(df, 12.752, "12.8");
    roundingTest(df, 12.85, "12.8");
    roundingTest(df, 12.852, "12.9");
    roundingTest(df, 12.95, "13");
    roundingTest(df, 12.952, "13");
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 74 with DecimalFormat

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

the class NumberFormatRegressionTest method TestJB5509.

/*
     * Test case for JB#5509, strict parsing issue
     */
@Test
public void TestJB5509() {
    String[] data = { "1,2", "1.2", "1,2.5", "1,23.5", "1,234.5", "1,234", "1,234,567", "1,234,567.8", "1,234,5", "1,234,5.6", "1,234,56.7" };
    boolean[] expected = { // false for expected parse failure
    false, true, false, false, true, true, true, true, false, false, false, false };
    DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(new ULocale("en_US")));
    df.setParseStrict(true);
    for (int i = 0; i < data.length; i++) {
        try {
            df.parse(data[i]);
            if (!expected[i]) {
                errln("Failed: ParseException must be thrown for string " + data[i]);
            }
        } catch (ParseException pe) {
            if (expected[i]) {
                errln("Failed: ParseException must not be thrown for string " + data[i]);
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 75 with DecimalFormat

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

the class NumberFormatRegressionTest method TestNBSPInPattern.

@Test
public void TestNBSPInPattern() {
    NumberFormat nf = null;
    String testcase;
    testcase = "ar_AE UNUM_CURRENCY";
    nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE"));
    checkNBSPPatternRT(testcase, nf);
    // if we don't have CLDR 1.6 data, bring out the problem anyways
    String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00";
    testcase = "ar_AE special pattern: " + SPECIAL_PATTERN;
    nf = new DecimalFormat();
    ((DecimalFormat) nf).applyPattern(SPECIAL_PATTERN);
    checkNBSPPatternRT(testcase, nf);
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat) 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