Search in sources :

Example 46 with NumberFormat

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

the class MyNumberFormat method Test4071014.

/**
 * Data rounding errors for German (Germany) locale
 */
@Test
public void Test4071014() {
    NumberFormat formatter;
    String tempString;
    /* user error :
        String expectedDefault = "-5.789,987";
        String expectedCurrency = "5.789,98\u00a0DM";
        String expectedPercent = "-578.998%";
        */
    String expectedDefault = "-5.789,988";
    String expectedCurrency = "5.789,99\u00a0" + EURO;
    String expectedPercent = "-578.999\u00a0%";
    formatter = NumberFormat.getNumberInstance(Locale.GERMANY);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedDefault)) {
        logln("Bug 4071014 default test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedDefault + " Received " + tempString);
    }
    formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY);
    tempString = formatter.format(5789.9876);
    if (tempString.equals(expectedCurrency)) {
        logln("Bug 4071014 currency test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedCurrency + " Received " + tempString);
    }
    formatter = NumberFormat.getPercentInstance(Locale.GERMANY);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedPercent)) {
        logln("Bug 4071014 percentage test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedPercent + " Received " + tempString);
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 47 with NumberFormat

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

the class MyNumberFormat method Test4162852.

/**
 * NumberFormat does not parse negative zero.
 */
@Test
public void Test4162852() throws ParseException {
    for (int i = 0; i < 2; ++i) {
        NumberFormat f = (i == 0) ? NumberFormat.getInstance() : NumberFormat.getPercentInstance();
        double d = -0.0;
        String s = f.format(d);
        double e = f.parse(s).doubleValue();
        logln("" + d + " -> " + '"' + s + '"' + " -> " + e);
        if (e != 0.0 || 1.0 / e > 0.0) {
            logln("Failed to parse negative zero");
        }
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 48 with NumberFormat

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

the class MyNumberFormat method Test4070798.

/**
 * Number format data rounding errors for locale FR
 */
@Test
public void Test4070798() {
    NumberFormat formatter;
    String tempString;
    /* User error :
        String expectedDefault = "-5\u00a0789,987";
        String expectedCurrency = "5\u00a0789,98\u00a0F";
        String expectedPercent = "-578\u00a0998%";
        */
    String expectedDefault = "-5\u00a0789,988";
    // euro
    String expectedCurrency = "5\u00a0789,99\u00a0" + EURO;
    String expectedPercent = "-578\u00a0999\u00a0%";
    formatter = NumberFormat.getNumberInstance(Locale.FRANCE);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedDefault)) {
        logln("Bug 4070798 default test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedDefault + " Received " + tempString);
    }
    formatter = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    tempString = formatter.format(5789.9876);
    if (tempString.equals(expectedCurrency)) {
        logln("Bug 4070798 currency test assed.");
    } else {
        errln("Failed:" + " Expected " + expectedCurrency + " Received " + tempString);
    }
    formatter = NumberFormat.getPercentInstance(Locale.FRANCE);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedPercent)) {
        logln("Bug 4070798 percentage test passed.");
    } else {
        errln("Failed:" + " Expected " + expectedPercent + " Received " + tempString);
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 49 with NumberFormat

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

the class MyNumberFormat method Test4217661.

/**
 * DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction
 * digits.
 */
@Test
public void Test4217661() {
    Object[] DATA = { new Double(0.001), "0", new Double(1.001), "1", new Double(0.006), "0.01", new Double(1.006), "1.01" };
    NumberFormat fmt = NumberFormat.getInstance(Locale.US);
    fmt.setMaximumFractionDigits(2);
    for (int i = 0; i < DATA.length; i += 2) {
        String s = fmt.format(((Double) DATA[i]).doubleValue());
        if (!s.equals(DATA[i + 1])) {
            errln("FAIL: Got " + s + ", exp " + DATA[i + 1]);
        }
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 50 with NumberFormat

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

the class NumberFormatRegressionTest method TestSerialization.

// Test New serialized DecimalFormat(2.0) read old serialized forms of DecimalFormat(1.3.1.1)
@Test
public void TestSerialization() throws IOException {
    byte[][] contents = NumberFormatSerialTestData.getContent();
    double data = 1234.56;
    String[] expected = { "1,234.56", "$1,234.56", "123,456%", "1.23456E3" };
    for (int i = 0; i < 4; ++i) {
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(contents[i]));
        try {
            NumberFormat format = (NumberFormat) ois.readObject();
            String result = format.format(data);
            if (result.equals(expected[i])) {
                logln("OK: Deserialized bogus NumberFormat(new version read old version)");
            } else {
                errln("FAIL: the test data formats are not euqal");
            }
        } catch (Exception e) {
            warnln("FAIL: " + e.getMessage());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ParseException(java.text.ParseException) ObjectInputStream(java.io.ObjectInputStream) 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