Search in sources :

Example 51 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class OldDecimalFormatSymbolsTest method test_hashCode.

public void test_hashCode() {
    DecimalFormatSymbols dfs1 = new DecimalFormatSymbols();
    DecimalFormatSymbols dfs2 = (DecimalFormatSymbols) dfs1.clone();
    assertTrue("Hash codes of equal object are equal", dfs2.hashCode() == dfs1.hashCode());
    dfs1.setInfinity("infinity_infinity");
    assertTrue("Hash codes of non-equal objects are equal", dfs2.hashCode() != dfs1.hashCode());
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Example 52 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class OldDecimalFormatSymbolsTest method test_RIHarmony_compatible.

public void test_RIHarmony_compatible() throws Exception {
    ObjectInputStream i = null;
    try {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRANCE);
        i = new ObjectInputStream(getClass().getClassLoader().getResourceAsStream("serialization/java/text/DecimalFormatSymbols.ser"));
        DecimalFormatSymbols riSymbols = (DecimalFormatSymbols) i.readObject();
        // RI's default NaN is U+FFFD, Harmony's is based on ICU
        // This suggests an RI bug, assuming that non-UTF8 bytes are UTF8 and
        // getting a conversion failure.
        riSymbols.setNaN("NaN");
        assertEquals(symbols, riSymbols);
    } catch (NullPointerException e) {
        assertNotNull("Failed to load /serialization/java/text/" + "DecimalFormatSymbols.ser", i);
    } finally {
        try {
            if (i != null) {
                i.close();
            }
        } catch (Exception e) {
        }
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) ObjectInputStream(java.io.ObjectInputStream)

Example 53 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class OldDecimalFormatTest method test_toLocalizedPattern.

public void test_toLocalizedPattern() {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    try {
        format.applyLocalizedPattern("#.#");
        assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern());
        format.applyLocalizedPattern("#.");
        assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern());
        format.applyLocalizedPattern("#");
        assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern());
        format.applyLocalizedPattern(".#");
        assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern());
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) Support_DecimalFormat(tests.support.Support_DecimalFormat) DecimalFormat(java.text.DecimalFormat) ParseException(java.text.ParseException)

Example 54 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project jgnash by ccavanaugh.

the class DefaultCurrencies method buildNode.

/**
     * Creates a valid CurrencyNode given a locale.
     *
     * @param locale Locale to create a CurrencyNode for
     * @return The new CurrencyNode
     */
public static CurrencyNode buildNode(final Locale locale) {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
    Currency c = symbols.getCurrency();
    CurrencyNode node = new CurrencyNode();
    node.setSymbol(c.getCurrencyCode());
    node.setPrefix(symbols.getCurrencySymbol());
    node.setScale((byte) c.getDefaultFractionDigits());
    return node;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) Currency(java.util.Currency)

Example 55 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project jgnash by ccavanaugh.

the class CommodityFormat method getFullNumberFormat.

/**
     * Returns a thread safe {@code NumberFormat} for a given {@code CommodityNode}.
     *
     * @param node CommodityNode to format to
     * @return thread safe {@code NumberFormat}
     */
public static NumberFormat getFullNumberFormat(@NotNull final CommodityNode node) {
    final ThreadLocal<DecimalFormat> o = fullInstanceMap.get(node);
    if (o != null) {
        return o.get();
    }
    final ThreadLocal<DecimalFormat> threadLocal = ThreadLocal.withInitial(() -> {
        final DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance();
        final DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        dfs.setCurrencySymbol(node.getPrefix());
        df.setDecimalFormatSymbols(dfs);
        df.setMaximumFractionDigits(node.getScale());
        // required for some locale
        df.setMinimumFractionDigits(df.getMaximumFractionDigits());
        if (node.getSuffix() != null && !node.getSuffix().isEmpty()) {
            df.setPositiveSuffix(node.getSuffix() + df.getPositiveSuffix());
            df.setNegativeSuffix(node.getSuffix() + df.getNegativeSuffix());
        }
        // for positive suffix padding for fraction alignment
        final int negSufLen = df.getNegativeSuffix().length();
        final int posSufLen = df.getPositiveSuffix().length();
        if (negSufLen > posSufLen) {
            StringBuilder buf = new StringBuilder(df.getPositiveSuffix());
            for (int i = negSufLen - posSufLen; i <= negSufLen; i++) {
                buf.append(' ');
            }
            df.setPositiveSuffix(buf.toString());
        } else if (posSufLen > negSufLen) {
            StringBuilder buf = new StringBuilder(df.getNegativeSuffix());
            for (int i = posSufLen - negSufLen; i <= posSufLen; i++) {
                buf.append(' ');
            }
            df.setNegativeSuffix(buf.toString());
        }
        return df;
    });
    fullInstanceMap.put(node, threadLocal);
    return threadLocal.get();
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Aggregations

DecimalFormatSymbols (java.text.DecimalFormatSymbols)133 DecimalFormat (java.text.DecimalFormat)93 NumberFormat (java.text.NumberFormat)14 Locale (java.util.Locale)13 ParseException (java.text.ParseException)8 BigDecimal (java.math.BigDecimal)7 Currency (java.util.Currency)7 ObjectInputStream (java.io.ObjectInputStream)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 ParsePosition (java.text.ParsePosition)5 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)5 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)5 IndexInput (org.apache.lucene.store.IndexInput)5 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 BytesRef (org.apache.lucene.util.BytesRef)4 ArrayList (java.util.ArrayList)3