Search in sources :

Example 11 with DecimalFormat

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

the class NumberFormatSpecificationTest method formatFrWithPattern.

private static String formatFrWithPattern(double d, String pattern) {
    DecimalFormatSymbols sym = new DecimalFormatSymbols(ULocale.FRANCE);
    DecimalFormat fmt = new DecimalFormat(pattern, sym);
    return fmt.format(d).replace('\u00a0', ' ');
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat)

Example 12 with DecimalFormat

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

the class TestMessageFormat method testNumericOnlyMethods.

// Ensure that methods designed for numeric arguments only, will throw
// an exception when called on MessageFormat objects created with
// named arguments.
@Test
public void testNumericOnlyMethods() {
    MessageFormat msg = new MessageFormat("Number of files: {numfiles}");
    boolean gotException = false;
    try {
        Format[] fmts = { new DecimalFormat() };
        msg.setFormatsByArgumentIndex(fmts);
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        errln("MessageFormat.setFormatsByArgumentIndex() should throw an " + "IllegalArgumentException when called on formats with " + "named arguments but did not!");
    }
    gotException = false;
    try {
        msg.setFormatByArgumentIndex(0, new DecimalFormat());
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        errln("MessageFormat.setFormatByArgumentIndex() should throw an " + "IllegalArgumentException when called on formats with " + "named arguments but did not!");
    }
    gotException = false;
    try {
        msg.getFormatsByArgumentIndex();
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        errln("MessageFormat.getFormatsByArgumentIndex() should throw an " + "IllegalArgumentException when called on formats with " + "named arguments but did not!");
    }
    gotException = false;
    try {
        Object[] args = { new Long(42) };
        msg.format(args, new StringBuffer(), new FieldPosition(0));
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        errln("MessageFormat.format(Object[], StringBuffer, FieldPosition) " + "should throw an IllegalArgumentException when called on " + "formats with named arguments but did not!");
    }
    gotException = false;
    try {
        Object[] args = { new Long(42) };
        msg.format((Object) args, new StringBuffer(), new FieldPosition(0));
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        errln("MessageFormat.format(Object, StringBuffer, FieldPosition) " + "should throw an IllegalArgumentException when called with " + "non-Map object as argument on formats with named " + "arguments but did not!");
    }
    gotException = false;
    try {
        msg.parse("Number of files: 5", new ParsePosition(0));
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        errln("MessageFormat.parse(String, ParsePosition) " + "should throw an IllegalArgumentException when called with " + "non-Map object as argument on formats with named " + "arguments but did not!");
    }
    gotException = false;
    try {
        msg.parse("Number of files: 5");
    } catch (IllegalArgumentException e) {
        gotException = true;
    } catch (ParseException e) {
        errln("Wrong exception thrown.");
    }
    if (!gotException) {
        errln("MessageFormat.parse(String) " + "should throw an IllegalArgumentException when called with " + "non-Map object as argument on formats with named " + "arguments but did not!");
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) UFormat(android.icu.text.UFormat) Format(java.text.Format) MessageFormat(android.icu.text.MessageFormat) DateFormat(android.icu.text.DateFormat) NumberFormat(android.icu.text.NumberFormat) DecimalFormat(android.icu.text.DecimalFormat) ChoiceFormat(java.text.ChoiceFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 13 with DecimalFormat

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

the class TestMessageFormat method TestSetFormat.

@Test
public void TestSetFormat() {
    MessageFormat ms = new MessageFormat("{number} {date}", ULocale.ENGLISH);
    final DecimalFormat decimalFormat = new DecimalFormat("000.000", DecimalFormatSymbols.getInstance(ULocale.ENGLISH));
    ms.setFormatByArgumentName("number", decimalFormat);
    final SimpleDateFormat dateFormat = new SimpleDateFormat("'year:'yy 'month:'MM 'day:'dd");
    dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
    ms.setFormatByArgumentName("date", dateFormat);
    Map map = new HashMap();
    map.put("number", new Integer(1234));
    map.put("date", new Date(0, 0, 0));
    String result = ms.format(map);
    assertEquals("setFormatByArgumentName", "1234.000 year:99 month:12 day:31", result);
    Set formatNames = ms.getArgumentNames();
    assertEquals("Format Names match", formatNames, map.keySet());
    assertEquals("Decimal", decimalFormat, ms.getFormatByArgumentName("number"));
    assertEquals("Date", dateFormat, ms.getFormatByArgumentName("date"));
}
Also used : Set(java.util.Set) MessageFormat(android.icu.text.MessageFormat) HashMap(java.util.HashMap) DecimalFormat(android.icu.text.DecimalFormat) AttributedString(java.text.AttributedString) SimpleDateFormat(android.icu.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Date(java.util.Date) Test(org.junit.Test)

Example 14 with DecimalFormat

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

the class ScientificNumberFormatterTest method TestFixedDecimalSuperscript.

@Test
public void TestFixedDecimalSuperscript() {
    DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(ULocale.ENGLISH);
    ScientificNumberFormatter fmt = ScientificNumberFormatter.getSuperscriptInstance(decfmt);
    assertEquals("", "123,456", fmt.format(123456.0));
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) ScientificNumberFormatter(android.icu.text.ScientificNumberFormatter) Test(org.junit.Test)

Example 15 with DecimalFormat

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

the class BigNumberFormatTest method Test4161100.

@Test
public void Test4161100() {
    NumberFormat f = NumberFormat.getInstance();
    f.setMinimumFractionDigits(1);
    f.setMaximumFractionDigits(1);
    double a = -0.09;
    String s = f.format(a);
    logln(a + " x " + ((DecimalFormat) f).toPattern() + " = " + s);
    if (!s.equals("-0.1")) {
        errln("FAIL");
    }
}
Also used : 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