Search in sources :

Example 56 with DecimalFormatSymbols

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

the class CommodityFormat method getShortNumberFormat.

/**
     *
     * @param scale scale of the simple number
     * @return thread safe {@code NumberFormat}
     */
public static NumberFormat getShortNumberFormat(final int scale) {
    final ThreadLocal<DecimalFormat> o = simpleInstanceMap.get(scale);
    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("");
        df.setDecimalFormatSymbols(dfs);
        df.setMaximumFractionDigits(scale);
        // required for some locale
        df.setMinimumFractionDigits(df.getMaximumFractionDigits());
        // for positive suffix padding for fraction alignment
        int negSufLen = df.getNegativeSuffix().length();
        if (negSufLen > 0) {
            char[] pad = new char[negSufLen];
            for (int i = 0; i < negSufLen; i++) {
                pad[i] = ' ';
            }
            df.setPositiveSuffix(new String(pad));
        }
        return df;
    });
    simpleInstanceMap.put(scale, threadLocal);
    return threadLocal.get();
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 57 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project jdk8u_jdk by JetBrains.

the class NumberFormatProviderImpl method getInstance.

private NumberFormat getInstance(Locale locale, int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }
    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }
    return format;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 58 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project tika by apache.

the class OOXMLParserTest method testBigIntegersWGeneralFormat.

@Test
public void testBigIntegersWGeneralFormat() throws Exception {
    //TIKA-2025
    String xml = getXML("testEXCEL_big_numbers.xlsx").xml;
    //15 digit number
    assertContains("123456789012345", xml);
    //15 digit formula
    assertContains("123456789012346", xml);
    Locale locale = LocaleUtil.getUserLocale();
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
    //16 digit number is treated as scientific notation as is the 16 digit formula
    assertContains("1" + symbols.getDecimalSeparator() + "23456789012345E+15</td>\t" + "<td>1" + symbols.getDecimalSeparator() + "23456789012345E+15", xml);
}
Also used : Locale(java.util.Locale) DecimalFormatSymbols(java.text.DecimalFormatSymbols) ExcelParserTest(org.apache.tika.parser.microsoft.ExcelParserTest) Test(org.junit.Test) TikaTest(org.apache.tika.TikaTest) WordParserTest(org.apache.tika.parser.microsoft.WordParserTest)

Example 59 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project symja_android_library by axkr.

the class NumberTest method testNumberFormat.

/**
	 * Format a double value with a <code>java.text.DecimalFormat</code> object.
	 */
public void testNumberFormat() {
    StringBuilder buf = new StringBuilder();
    try {
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
        DecimalFormat decimalFormat = new DecimalFormat("0.0####", otherSymbols);
        OutputFormFactory factory = OutputFormFactory.get(true, false, decimalFormat);
        IExpr expr = F.num("12345.123456789");
        factory.convert(buf, expr);
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertEquals(buf.toString(), "12345.12346");
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) IExpr(org.matheclipse.core.interfaces.IExpr) IOException(java.io.IOException) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory)

Example 60 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project cytoscape-api by cytoscape.

the class ObjectPosition method shortString.

private String shortString() {
    // force the locale to US so that we consistently serialize
    final DecimalFormat df = new DecimalFormat("#0.00;-#0.00", new DecimalFormatSymbols(Locale.US));
    final StringBuilder sb = new StringBuilder();
    sb.append(targetAnchor.getShortName());
    sb.append(",");
    sb.append(objectAnchor.getShortName());
    sb.append(",");
    sb.append(justify.getShortName());
    sb.append(",");
    sb.append(df.format(xOffset));
    sb.append(",");
    sb.append(df.format(yOffset));
    return sb.toString();
}
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