Search in sources :

Example 31 with NumberFormat

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

the class NumberFormatTest method test_setCurrency.

// Test the currency symbol is correctly taken from ICU. Verifies that the fractional digits
// are not updated because DecimalFormat.setCurrency agrees not to change it.
public void test_setCurrency() throws Exception {
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    // The Armenian Dram is a special case where the fractional digits are 0.
    Currency amd = Currency.getInstance("AMD");
    assertEquals(0, amd.getDefaultFractionDigits());
    // Armenian Dram ISO 4217 code.
    nf.setCurrency(amd);
    // Check DecimalFormat has not taken the
    assertEquals(2, nf.getMinimumFractionDigits());
    // currency specific fractional digits.
    assertEquals(2, nf.getMaximumFractionDigits());
    assertEquals("AMD50.00", nf.format(50.00));
    // Try and explicitly request fractional digits for the specified currency.
    nf.setMaximumFractionDigits(amd.getDefaultFractionDigits());
    assertEquals("AMD50", nf.format(50.00));
    nf = NumberFormat.getCurrencyInstance(Locale.US);
    // Euro sign.
    nf.setCurrency(Currency.getInstance("EUR"));
    assertEquals("€50.00", nf.format(50.00));
    // Japanese Yen symbol.
    nf.setCurrency(Currency.getInstance("JPY"));
    assertEquals("¥50.00", nf.format(50.00));
    // Swiss Franc ISO 4217 code.
    nf.setCurrency(Currency.getInstance("CHF"));
    assertEquals("CHF50.00", nf.format(50.00));
}
Also used : Currency(java.util.Currency) NumberFormat(java.text.NumberFormat)

Example 32 with NumberFormat

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

the class NumberFormatTest method test_custom_Number_gets_longValue.

// NumberFormat.format(Object, StringBuffer, FieldPosition) guarantees it calls doubleValue for
// custom Number subclasses.
public void test_custom_Number_gets_longValue() throws Exception {
    class MyNumber extends Number {

        public byte byteValue() {
            throw new UnsupportedOperationException();
        }

        public double doubleValue() {
            return 123;
        }

        public float floatValue() {
            throw new UnsupportedOperationException();
        }

        public int intValue() {
            throw new UnsupportedOperationException();
        }

        public long longValue() {
            throw new UnsupportedOperationException();
        }

        public short shortValue() {
            throw new UnsupportedOperationException();
        }

        public String toString() {
            throw new UnsupportedOperationException();
        }
    }
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
    assertEquals("123", nf.format(new MyNumber()));
}
Also used : NumberFormat(java.text.NumberFormat)

Example 33 with NumberFormat

use of java.text.NumberFormat 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 34 with NumberFormat

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

the class NumberFormatTest method test_currencyFromLocale.

// Test to ensure currency formatting from specified locale works.
public void test_currencyFromLocale() {
    // French locale formats with "," as separator and Euro symbol after a non-breaking space.
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    assertEquals("50,00 €", nf.format(50));
    // British locale uses pound sign with no spacing.
    nf = NumberFormat.getCurrencyInstance(Locale.UK);
    assertEquals("£50.00", nf.format(50));
}
Also used : NumberFormat(java.text.NumberFormat)

Example 35 with NumberFormat

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

the class NumberFormatTest method test_small_BigInteger_gets_longValue.

// NumberFormat.format(Object, StringBuffer, FieldPosition) guarantees it calls longValue for
// any BigInteger with a bitLength strictly less than 64.
public void test_small_BigInteger_gets_longValue() throws Exception {
    class MyNumberFormat extends NumberFormat {

        public StringBuffer format(double value, StringBuffer b, FieldPosition f) {
            b.append("double");
            return b;
        }

        public StringBuffer format(long value, StringBuffer b, FieldPosition f) {
            b.append("long");
            return b;
        }

        public Number parse(String string, ParsePosition p) {
            throw new UnsupportedOperationException();
        }
    }
    NumberFormat nf = new MyNumberFormat();
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MAX_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)));
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MIN_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)));
}
Also used : FieldPosition(java.text.FieldPosition) ParsePosition(java.text.ParsePosition) NumberFormat(java.text.NumberFormat)

Aggregations

NumberFormat (java.text.NumberFormat)471 DecimalFormat (java.text.DecimalFormat)92 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)24 BigDecimal (java.math.BigDecimal)23 Locale (java.util.Locale)22 Map (java.util.Map)18 Test (org.junit.Test)17 ParseException (java.text.ParseException)16 DecimalFormatSymbols (java.text.DecimalFormatSymbols)14 JFreeChart (org.jfree.chart.JFreeChart)13 IOException (java.io.IOException)12 ParsePosition (java.text.ParsePosition)12 XYSeries (org.jfree.data.xy.XYSeries)11 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)11 Intent (android.content.Intent)10 PrintWriter (java.io.PrintWriter)9 View (android.view.View)8 TextView (android.widget.TextView)8 Currency (java.util.Currency)8