Search in sources :

Example 76 with NumberFormat

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

the class PluralRulesTest method TestBasicFraction.

@Test
public void TestBasicFraction() {
    String[][] tests = { { "en", "one: j is 1" }, { "1", "0", "1", "one" }, { "1", "2", "1.00", "other" } };
    ULocale locale = null;
    NumberFormat nf = null;
    PluralRules pr = null;
    for (String[] row : tests) {
        switch(row.length) {
            case 2:
                locale = ULocale.forLanguageTag(row[0]);
                nf = NumberFormat.getInstance(locale);
                pr = PluralRules.createRules(row[1]);
                break;
            case 4:
                double n = Double.parseDouble(row[0]);
                int minFracDigits = Integer.parseInt(row[1]);
                nf.setMinimumFractionDigits(minFracDigits);
                String expectedFormat = row[2];
                String expectedKeyword = row[3];
                UFieldPosition pos = new UFieldPosition();
                String formatted = nf.format(1.0, new StringBuffer(), pos).toString();
                int countVisibleFractionDigits = pos.getCountVisibleFractionDigits();
                long fractionDigits = pos.getFractionDigits();
                String keyword = pr.select(n, countVisibleFractionDigits, fractionDigits);
                assertEquals("Formatted " + n + "\t" + minFracDigits, expectedFormat, formatted);
                assertEquals("Keyword " + n + "\t" + minFracDigits, expectedKeyword, keyword);
                break;
            default:
                throw new RuntimeException();
        }
    }
}
Also used : PluralRules(android.icu.text.PluralRules) ULocale(android.icu.util.ULocale) UFieldPosition(android.icu.text.UFieldPosition) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 77 with NumberFormat

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

the class MyNumberFormat method test4233840.

/**
 * 4233840: NumberFormat does not round correctly
 */
@Test
public void test4233840() {
    float f = 0.0099f;
    NumberFormat nf = new DecimalFormat("0.##", new DecimalFormatSymbols(Locale.US));
    nf.setMinimumFractionDigits(2);
    String result = nf.format(f);
    if (!result.equals("0.01")) {
        errln("FAIL: input: " + f + ", expected: 0.01, got: " + result);
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 78 with NumberFormat

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

the class MyNumberFormat method assignFloatValue.

public float assignFloatValue(float returnfloat) {
    logln(" VALUE " + returnfloat);
    NumberFormat nfcommon = NumberFormat.getCurrencyInstance(Locale.US);
    nfcommon.setGroupingUsed(false);
    String stringValue = nfcommon.format(returnfloat).substring(1);
    if (Float.valueOf(stringValue).floatValue() != returnfloat)
        errln(" DISPLAYVALUE " + stringValue);
    return returnfloat;
}
Also used : NumberFormat(android.icu.text.NumberFormat)

Example 79 with NumberFormat

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

the class MyNumberFormat method Test4114639.

/**
 * NumberFormat.parse doesn't return null
 */
@Test
public void Test4114639() {
    NumberFormat format = NumberFormat.getInstance();
    String text = "time 10:x";
    ParsePosition pos = new ParsePosition(8);
    Number result = format.parse(text, pos);
    // Should be null; it isn't
    if (result != null)
        errln("Should return null but got : " + result);
}
Also used : NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 80 with NumberFormat

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

the class MyNumberFormat method Test4098741.

/**
 * Tests the setMaximumFractionDigits limit.
 */
@Test
public void Test4098741() {
    try {
        NumberFormat fmt = NumberFormat.getPercentInstance();
        fmt.setMaximumFractionDigits(20);
        logln(fmt.format(.001));
    } catch (Exception foo) {
        warnln("Bug 4098471 failed with exception thrown : " + foo.getMessage());
    }
}
Also used : InvalidObjectException(java.io.InvalidObjectException) ParseException(java.text.ParseException) IOException(java.io.IOException) 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