Search in sources :

Example 31 with NumberFormat

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

the class MyNumberFormat method Test4071492.

/**
 * DecimalFormat does not round up correctly.
 */
@Test
public void Test4071492() {
    double x = 0.00159999;
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    String out = nf.format(x);
    logln("0.00159999 formats with 4 fractional digits to " + out);
    String expected = "0.0016";
    if (!out.equals(expected))
        errln("FAIL: Expected " + expected);
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 32 with NumberFormat

use of android.icu.text.NumberFormat 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 33 with NumberFormat

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

the class MyNumberFormat method Test4167494.

/**
 * NumberFormat truncates data
 */
@Test
public void Test4167494() throws Exception {
    NumberFormat fmt = NumberFormat.getInstance(Locale.US);
    double a = Double.MAX_VALUE;
    String s = fmt.format(a);
    double b = fmt.parse(s).doubleValue();
    boolean match = a == b;
    if (match) {
        logln("" + a + " -> \"" + s + "\" -> " + b + " ok");
    } else {
        errln("" + a + " -> \"" + s + "\" -> " + b + " FAIL");
    }
    // in case we want to address this later.
    if (false) {
        a = Double.MIN_VALUE;
        s = fmt.format(a);
        b = fmt.parse(s).doubleValue();
        match = a == b;
        if (match) {
            logln("" + a + " -> \"" + s + "\" -> " + b + " ok");
        } else {
            errln("" + a + " -> \"" + s + "\" -> " + b + " FAIL");
        }
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 34 with NumberFormat

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

the class MyNumberFormat method Test4071005.

/**
 * Data rounding errors for French (Canada) locale
 */
@Test
public void Test4071005() {
    NumberFormat formatter;
    String tempString;
    /* user error :
        String expectedDefault = "-5 789,987";
        String expectedCurrency = "5 789,98\u00a0$";
        String expectedPercent = "-578 998%";
    */
    String expectedDefault = "-5\u00a0789,988";
    String expectedCurrency = "5\u00a0789,99\u00a0$";
    String expectedPercent = "-578\u00a0999\u00A0%";
    formatter = NumberFormat.getNumberInstance(Locale.CANADA_FRENCH);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedDefault)) {
        logln("Bug 4071005 default test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedDefault + " Received " + tempString);
    }
    formatter = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
    tempString = formatter.format(5789.9876);
    if (tempString.equals(expectedCurrency)) {
        logln("Bug 4071005 currency test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedCurrency + " Received " + tempString);
    }
    formatter = NumberFormat.getPercentInstance(Locale.CANADA_FRENCH);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedPercent)) {
        logln("Bug 4071005 percentage test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedPercent + " Received " + tempString);
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 35 with NumberFormat

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

the class PluralFormatUnitTest method TestConstructor.

@Test
public void TestConstructor() {
    // Test correct formatting of numbers.
    PluralFormat[] plFmts = new PluralFormat[10];
    plFmts[0] = new PluralFormat();
    plFmts[0].applyPattern("other{#}");
    plFmts[1] = new PluralFormat(PluralRules.DEFAULT);
    plFmts[1].applyPattern("other{#}");
    plFmts[2] = new PluralFormat(PluralRules.DEFAULT, "other{#}");
    plFmts[3] = new PluralFormat("other{#}");
    plFmts[4] = new PluralFormat(ULocale.getDefault());
    plFmts[4].applyPattern("other{#}");
    plFmts[5] = new PluralFormat(ULocale.getDefault(), PluralRules.DEFAULT);
    plFmts[5].applyPattern("other{#}");
    plFmts[6] = new PluralFormat(ULocale.getDefault(), PluralRules.DEFAULT, "other{#}");
    plFmts[7] = new PluralFormat(ULocale.getDefault(), "other{#}");
    // Constructors with Java Locale
    plFmts[8] = new PluralFormat(Locale.getDefault());
    plFmts[8].applyPattern("other{#}");
    plFmts[9] = new PluralFormat(Locale.getDefault(), PluralRules.DEFAULT);
    plFmts[9].applyPattern("other{#}");
    // These plural formats should produce the same output as a
    // NumberFormat for the default locale.
    NumberFormat numberFmt = NumberFormat.getInstance(ULocale.getDefault());
    for (int n = 1; n < 13; n++) {
        String result = numberFmt.format(n);
        for (int k = 0; k < plFmts.length; ++k) {
            TestFmwk.assertEquals("PluralFormat's output is not as expected", result, plFmts[k].format(n));
        }
    }
    // Test some bigger numbers.
    // Coverage: Use the format(Object, ...) version.
    StringBuffer sb = new StringBuffer();
    FieldPosition ignore = new FieldPosition(-1);
    for (int n = 100; n < 113; n++) {
        String result = numberFmt.format(n * n);
        for (int k = 0; k < plFmts.length; ++k) {
            sb.delete(0, sb.length());
            String pfResult = plFmts[k].format(Long.valueOf(n * n), sb, ignore).toString();
            TestFmwk.assertEquals("PluralFormat's output is not as expected", result, pfResult);
        }
    }
}
Also used : PluralFormat(android.icu.text.PluralFormat) FieldPosition(java.text.FieldPosition) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Aggregations

NumberFormat (android.icu.text.NumberFormat)105 Test (org.junit.Test)84 ULocale (android.icu.util.ULocale)39 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)35 DecimalFormat (android.icu.text.DecimalFormat)27 Locale (java.util.Locale)24 ParseException (java.text.ParseException)20 ParsePosition (java.text.ParsePosition)13 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)12 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)11 FieldPosition (java.text.FieldPosition)9 IOException (java.io.IOException)7 DateFormat (android.icu.text.DateFormat)5 Calendar (android.icu.util.Calendar)5 Date (java.util.Date)5 BigDecimal (android.icu.math.BigDecimal)4 DisplayContext (android.icu.text.DisplayContext)4 MeasureFormat (android.icu.text.MeasureFormat)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 PluralFormat (android.icu.text.PluralFormat)3