Search in sources :

Example 66 with DecimalFormat

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

the class MyNumberFormat method Test4243011.

/**
 * 4243011: Formatting .5 rounds to "1" instead of "0"
 */
@Test
public void Test4243011() {
    double[] DATA = { 0.5, 1.5, 2.5, 3.5, 4.5 };
    String[] EXPECTED = { "0.", "2.", "2.", "4.", "4." };
    DecimalFormat format = new DecimalFormat("0.");
    for (int i = 0; i < DATA.length; i++) {
        String result = format.format(DATA[i]);
        if (result.equals(EXPECTED[i])) {
            logln("OK: got " + result);
        } else {
            errln("FAIL: got " + result);
        }
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 67 with DecimalFormat

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

the class MyNumberFormat method Test4106664.

/**
 * DecimalFormat.format(long n) fails if n * multiplier > MAX_LONG.
 */
@Test
public void Test4106664() {
    DecimalFormat df = new DecimalFormat();
    long n = 1234567890123456L;
    int m = 12345678;
    BigInteger bigN = BigInteger.valueOf(n);
    bigN = bigN.multiply(BigInteger.valueOf(m));
    df.setMultiplier(m);
    df.setGroupingUsed(false);
    logln("formated: " + df.format(n, new StringBuffer(), new FieldPosition(0)));
    logln("expected: " + bigN.toString());
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) BigInteger(java.math.BigInteger) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 68 with DecimalFormat

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

the class MyNumberFormat method Test4179818.

/**
 * DecimalFormat is incorrectly rounding numbers like 1.2501 to 1.2
 */
@Test
public void Test4179818() {
    String[] DATA = { // Input  Pattern  Expected output
    "1.2511", "#.#", "1.3", "1.2501", "#.#", "1.3", "0.9999", "#", "1" };
    DecimalFormat fmt = new DecimalFormat("#", new DecimalFormatSymbols(Locale.US));
    for (int i = 0; i < DATA.length; i += 3) {
        double in = Double.valueOf(DATA[i]).doubleValue();
        String pat = DATA[i + 1];
        String exp = DATA[i + 2];
        fmt.applyPattern(pat);
        String out = fmt.format(in);
        if (out.equals(exp)) {
            logln("Ok: " + in + " x " + pat + " = " + out);
        } else {
            errln("FAIL: " + in + " x  " + pat + " = " + out + ", expected " + exp);
        }
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 69 with DecimalFormat

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

the class MyNumberFormat method Test4122840.

/**
 * Locale data should use generic currency symbol
 *
 * 1) Make sure that all currency formats use the generic currency symbol.
 * 2) Make sure we get the same results using the generic symbol or a
 *    hard-coded one.
 */
@Test
public void Test4122840() {
    Locale[] locales = NumberFormat.getAvailableLocales();
    for (int i = 0; i < locales.length; i++) {
        ICUResourceBundle rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locales[i]);
        // 
        // Get the currency pattern for this locale.  We have to fish it
        // out of the ResourceBundle directly, since DecimalFormat.toPattern
        // will return the localized symbol, not \00a4
        // 
        String pattern = rb.getStringWithFallback("NumberElements/latn/patterns/currencyFormat");
        if (pattern.indexOf('\u00A4') == -1) {
            // 'x' not "x" -- workaround bug in IBM JDK 1.4.1
            errln("Currency format for " + locales[i] + " does not contain generic currency symbol:" + pattern);
        }
        // Create a DecimalFormat using the pattern we got and format a number
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(locales[i]);
        DecimalFormat fmt1 = new DecimalFormat(pattern, symbols);
        String result1 = fmt1.format(1.111);
        // 
        // Now substitute in the locale's currency symbol and create another
        // pattern.  Replace the decimal separator with the monetary separator.
        // 
        // char decSep = symbols.getDecimalSeparator(); //The variable is never used
        char monSep = symbols.getMonetaryDecimalSeparator();
        StringBuffer buf = new StringBuffer(pattern);
        for (int j = 0; j < buf.length(); j++) {
            if (buf.charAt(j) == '\u00a4') {
                String cur = "'" + symbols.getCurrencySymbol() + "'";
                buf.replace(j, j + 1, cur);
                j += cur.length() - 1;
            }
        }
        symbols.setDecimalSeparator(monSep);
        DecimalFormat fmt2 = new DecimalFormat(buf.toString(), symbols);
        // Actual width of decimal fractions and rounding option are inherited
        // from the currency, not the pattern itself.  So we need to force
        // maximum/minimumFractionDigits and rounding option for the second
        // DecimalForamt instance.  The fix for ticket#7282 requires this test
        // code change to make it work properly.
        fmt2.setMaximumFractionDigits(fmt1.getMaximumFractionDigits());
        fmt2.setMinimumFractionDigits(fmt1.getMinimumFractionDigits());
        fmt2.setRoundingIncrement(fmt1.getRoundingIncrement());
        String result2 = fmt2.format(1.111);
        // NOTE: en_IN is a special case (ChoiceFormat currency display name)
        if (!result1.equals(result2) && !locales[i].toString().equals("en_IN")) {
            errln("Results for " + locales[i] + " differ: " + result1 + " vs " + result2);
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

Example 70 with DecimalFormat

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

the class MyNumberFormat method Test4052223.

/**
 * Tests ParsePosition.setErrorPosition() and ParsePosition.getErrorPosition().
 */
@Test
public void Test4052223() {
    try {
        DecimalFormat fmt = new DecimalFormat("#,#00.00");
        Number num = fmt.parse("abc3");
        errln("Bug 4052223 failed : can't parse string \"a\".  Got " + num);
    } catch (ParseException foo) {
        logln("Caught expected ParseException : " + foo.getMessage() + " at index : " + foo.getErrorOffset());
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) 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