Search in sources :

Example 71 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method test_getIntegerInstanceLjava_util_Locale.

/**
     * @tests java.text.NumberFormat#getIntegerInstance(java.util.Locale)
     */
public void test_getIntegerInstanceLjava_util_Locale() throws ParseException {
    // Test for method java.text.NumberFormat
    // getIntegerInstance(java.util.Locale)
    Locale usLocale = Locale.US;
    Locale arLocale = new Locale("ar", "AE");
    DecimalFormat format = (DecimalFormat) NumberFormat.getIntegerInstance(usLocale);
    assertEquals("Test1: NumberFormat.getIntegerInstance().toPattern() returned wrong pattern", "#,##0", format.toPattern());
    assertEquals("Test2: NumberFormat.getIntegerInstance().format(-35.76) returned wrong value", "-36", format.format(-35.76));
    assertEquals("Test3: NumberFormat.getIntegerInstance().parse(\"-36\") returned wrong number", new Long(-36), format.parse("-36"));
    assertEquals("Test4: NumberFormat.getIntegerInstance().parseObject(\"-36\") returned wrong number", new Long(-36), format.parseObject("-36"));
    assertEquals("Test5: NumberFormat.getIntegerInstance().getMaximumFractionDigits() returned wrong value", 0, format.getMaximumFractionDigits());
    assertTrue("Test6: NumberFormat.getIntegerInstance().isParseIntegerOnly() returned wrong value", format.isParseIntegerOnly());
    // try with a locale that has a different integer pattern
    format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale);
    // Previous versions of android use just the positive format string (ICU4C) although now we
    // use '<positive_format>;<negative_format>' because of ICU4J denormalization.
    String variant = (format.toPattern().indexOf(';') > 0) ? "#,##0;-#,##0" : "#,##0";
    assertEquals("Test7: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).toPattern() returned wrong pattern", variant, format.toPattern());
    /* J2ObjC: MacOS doesn't seem to have the Arabic symbols.
        assertEquals(
                "Test8: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).format(-6) returned wrong value",
                "-٦", format.format(-6));*/
    assertEquals("Test9: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parse(\"-36-\") returned wrong number", new Long(36), format.parse("36-"));
    assertEquals("Test10: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parseObject(\"36-\") returned wrong number", new Long(36), format.parseObject("36-"));
    assertEquals("Test11: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).getMaximumFractionDigits() returned wrong value", 0, format.getMaximumFractionDigits());
    assertTrue("Test12: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).isParseIntegerOnly() returned wrong value", format.isParseIntegerOnly());
}
Also used : Locale(java.util.Locale) DecimalFormat(java.text.DecimalFormat)

Example 72 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method test_getIntegerInstance.

/**
     * @tests java.text.NumberFormat#getIntegerInstance()
     */
public void test_getIntegerInstance() throws ParseException {
    // Test for method java.text.NumberFormat getIntegerInstance()
    Locale origLocale = Locale.getDefault();
    Locale.setDefault(Locale.US);
    DecimalFormat format = (DecimalFormat) NumberFormat.getIntegerInstance();
    assertEquals("Test1: NumberFormat.getIntegerInstance().toPattern() returned wrong pattern", "#,##0", format.toPattern());
    assertEquals("Test2: NumberFormat.getIntegerInstance().format(35.76) returned wrong value", "36", format.format(35.76));
    assertEquals("Test3: NumberFormat.getIntegerInstance().parse(\"35.76\") returned wrong number", new Long(35), format.parse("35.76"));
    assertEquals("Test4: NumberFormat.getIntegerInstance().parseObject(\"35.76\") returned wrong number", new Long(35), format.parseObject("35.76"));
    Locale.setDefault(origLocale);
}
Also used : Locale(java.util.Locale) DecimalFormat(java.text.DecimalFormat)

Example 73 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method test_issue79925.

// https://code.google.com/p/android/issues/detail?id=79925\
// When switching currency after having initialised a DecimalFormat instance to a currency,
// the symbols are missing.
public void test_issue79925() {
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    nf.setCurrency(Currency.getInstance("EUR"));
    assertEquals("€50.00", nf.format(50.0));
    DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols();
    decimalFormatSymbols.setCurrencySymbol("");
    ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols);
    assertEquals("50.00", nf.format(50.0));
    nf.setCurrency(Currency.getInstance("SGD"));
    assertEquals("SGD50.00", nf.format(50.0));
    nf.setCurrency(Currency.getInstance("SGD"));
    assertEquals("SGD50.00", nf.format(50.00));
    nf.setCurrency(Currency.getInstance("USD"));
    assertEquals("$50.00", nf.format(50.0));
    nf.setCurrency(Currency.getInstance("SGD"));
    assertEquals("SGD50.00", nf.format(50.0));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Example 74 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method test_getIntegerInstance_ar.

public void test_getIntegerInstance_ar() throws Exception {
    // Previous versions of android use just the positive format string (ICU4C) although now we
    // use '<positive_format>;<negative_format>' because of ICU4J denormalization.
    NumberFormat numberFormat = NumberFormat.getNumberInstance(new Locale("ar"));
    String patternNI = ((DecimalFormat) numberFormat).toPattern();
    assertTrue("#,##0.###;-#,##0.###".equals(patternNI) || "#,##0.###".equals(patternNI));
    NumberFormat integerFormat = NumberFormat.getIntegerInstance(new Locale("ar"));
    String patternII = ((DecimalFormat) integerFormat).toPattern();
    assertTrue("#,##0;-#,##0".equals(patternII) || "#,##0".equals(patternII));
}
Also used : Locale(java.util.Locale) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Example 75 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method test_customCurrencySymbol.

// Test to ensure explicitly setting a currency symbol will overwrite the defaults.
public void test_customCurrencySymbol() {
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    DecimalFormatSymbols dfs = ((DecimalFormat) nf).getDecimalFormatSymbols();
    dfs.setCurrencySymbol("SPECIAL");
    ((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
    assertEquals("SPECIAL3.14", nf.format(3.14));
    // Setting the currency again should reset the symbols.
    nf.setCurrency(Currency.getInstance("USD"));
    assertEquals("$3.14", nf.format(3.14));
    // Setting it back again should work.
    dfs.setCurrencySymbol("NEW");
    ((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
    assertEquals("NEW3.14", nf.format(3.14));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Aggregations

DecimalFormat (java.text.DecimalFormat)823 DecimalFormatSymbols (java.text.DecimalFormatSymbols)94 NumberFormat (java.text.NumberFormat)93 IOException (java.io.IOException)48 BigDecimal (java.math.BigDecimal)47 ArrayList (java.util.ArrayList)44 IFormatterTextCallBack (org.xclcharts.common.IFormatterTextCallBack)33 ParseException (java.text.ParseException)31 Test (org.junit.Test)31 File (java.io.File)29 IFormatterDoubleCallBack (org.xclcharts.common.IFormatterDoubleCallBack)29 Support_DecimalFormat (tests.support.Support_DecimalFormat)28 Date (java.util.Date)25 SimpleDateFormat (java.text.SimpleDateFormat)24 HashMap (java.util.HashMap)24 Locale (java.util.Locale)22 PrintWriter (java.io.PrintWriter)20 UsageVO (com.cloud.usage.UsageVO)19 List (java.util.List)18 JFreeChart (org.jfree.chart.JFreeChart)18