Example usage for java.text DecimalFormat DecimalFormat

List of usage examples for java.text DecimalFormat DecimalFormat

Introduction

In this page you can find the example usage for java.text DecimalFormat DecimalFormat.

Prototype

public DecimalFormat(String pattern, DecimalFormatSymbols symbols) 

Source Link

Document

Creates a DecimalFormat using the given pattern and symbols.

Usage

From source file:com.haulmont.chile.core.datatypes.impl.LongDatatype.java

@Override
public String format(Object value, Locale locale) {
    if (value == null) {
        return "";
    }//from  w w  w  . j ava2  s .  c  o m

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return format(value);
    }

    DecimalFormatSymbols formatSymbols = formatStrings.getFormatSymbols();
    NumberFormat format = new DecimalFormat(formatStrings.getIntegerFormat(), formatSymbols);
    return format.format(value);
}

From source file:MondrianConnector.java

public ArrayList<LinkedHashMap<String, String>> ExecuteQuery(Query queryObject) throws Exception {
    System.setProperty("mondrian.olap.SsasCompatibleNaming", "true");
    String connectionString = getConnectionString(queryObject);
    RolapConnection connection = (RolapConnection) DriverManager.getConnection(connectionString, null);
    mondrian.olap.Query query = connection.parseQuery(queryObject.getMdxQuery());
    Result result = connection.execute(query);
    ArrayList<LinkedHashMap<String, String>> data = new ArrayList<LinkedHashMap<String, String>>();
    DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    df.setMaximumFractionDigits(340); //340 = DecimalFormat.DOUBLE_FRACTION_DIGITS
    if (result.getAxes().length == 1) {
        //Only One Axis has come so
        ArrayList<String> measures = new ArrayList<String>();
        for (Position p : result.getAxes()[0].getPositions()) {
            measures.add(p.get(0).getUniqueName().toString());
        }/*from  www.java2 s  . c o  m*/
        LinkedHashMap<String, String> row = new LinkedHashMap<String, String>();
        for (int i = 0; i < measures.size(); i++) {

            Object value = result.getCell(new int[] { i }).getValue();
            if (value == null) {
                row.put(measures.get(i), null);
            } else if (value instanceof Integer) {
                row.put(measures.get(i), ((Integer) value).toString());
            } else if (value instanceof Double) {
                row.put(measures.get(i), df.format(value));
            } else {
                row.put(measures.get(i), value.toString());
            }
        }
        data.add(row);
    } else if (result.getAxes().length == 2) {
        ArrayList<String> measures = new ArrayList<String>();
        for (Position p : result.getAxes()[0].getPositions()) {
            measures.add(p.get(0).getUniqueName().toString());
        }
        ArrayList<ArrayList<DimensionItem>> dimensionItems = new ArrayList<ArrayList<DimensionItem>>();
        for (Position p : result.getAxes()[1].getPositions()) {
            ArrayList<DimensionItem> itemsAtRow = new ArrayList<DimensionItem>();
            for (Object item : p.toArray()) {
                RolapMemberBase member = (RolapMemberBase) item;
                itemsAtRow.add(new DimensionItem(member.getLevel().getHierarchy().toString(),
                        member.getCaption().toString()));
            }
            dimensionItems.add(itemsAtRow);
        }
        for (int ix = 0; ix < dimensionItems.size(); ix++) {
            LinkedHashMap<String, String> row = new LinkedHashMap<String, String>();
            for (DimensionItem item : dimensionItems.get(ix)) {
                row.put(item.getLevel(), item.getCaption());
            }
            for (int i = 0; i < measures.size(); i++) {
                Object value = result.getCell(new int[] { i, ix }).getValue();
                if (value == null) {
                    row.put(measures.get(i), "0");
                } else {
                    if (value instanceof Integer) {
                        row.put(measures.get(i), ((Integer) value).toString());
                    } else if (value instanceof Double) {
                        row.put(measures.get(i), df.format(value));
                    } else {
                        row.put(measures.get(i), value.toString());
                    }
                }
            }
            data.add(row);
        }
    }
    return data;
}

From source file:com.cloudmade.api.geometry.Point.java

@Override
public String toString() {
    DecimalFormat formatter = new DecimalFormat("0.#####", new DecimalFormatSymbols(Locale.US));
    return formatter.format(lat) + "," + formatter.format(lng);
}

From source file:com.receipts.services.ExportService.java

public InputStream createExportFile(Store store) {
    InputStream is = null;/*from w  w  w  . j a  v a 2  s. co  m*/

    String storeId = store.getStoreId();
    String storeIdStr = StringUtils.leftPad(storeId, 3, "0");

    Date storeDate = store.getStoreDate();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    String storeDateStr = sdf.format(storeDate);

    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');

    StringBuilder sb = new StringBuilder();
    for (Receipt receipt : store.getReceipts()) {
        if (receipt.isComplete()) {
            String receiptId = receipt.getId().toString();
            String receiptTime = receipt.getReceiptTime();

            for (Product product : receipt.getProducts()) {
                // Store Id: 3 chars
                storeIdStr = StringUtils.substring(storeIdStr, 0, 3);
                sb.append(storeIdStr);

                // Product Name: 32 chars
                String productName = StringUtils.substring(product.getProductName(), 0, 32);
                String productNameStr = StringUtils.rightPad(productName, 32);
                sb.append(productNameStr);

                String productPrice = new DecimalFormat("0000.00", dfs).format(product.getProductPrice());
                String productPriceStr = StringUtils.leftPad(productPrice, 7, "0");
                sb.append(productPriceStr);

                // Receipt Id: 10 chars
                String receiptIdStr = StringUtils.leftPad(StringUtils.substring(receiptId, 0, 10), 10);
                sb.append(receiptIdStr);

                sb.append(storeDateStr);

                sb.append(receiptTime);

                String productQuantity = new DecimalFormat("#0.000", dfs).format(product.getProductQuantity());
                String productQuantityStr = StringUtils.leftPad(productQuantity, 6, "0");
                sb.append(productQuantityStr);

                sb.append(System.getProperty("line.separator"));
            }
        }
    }

    is = IOUtils.toInputStream(sb.toString());

    return is;
}

From source file:com.haulmont.chile.core.datatypes.impl.BigDecimalDatatype.java

@Override
public String format(Object value, Locale locale) {
    if (value == null) {
        return "";
    }// w  ww  . j  a v  a  2 s . c om

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return format(value);
    }

    DecimalFormatSymbols formatSymbols = formatStrings.getFormatSymbols();
    NumberFormat format = new DecimalFormat(formatStrings.getDecimalFormat(), formatSymbols);
    return format.format(value);
}

From source file:com.haulmont.chile.core.datatypes.impl.NumberDatatype.java

/**
 * Creates non-localized format./*from  ww w .j  a  va  2  s  .  com*/
 */
protected NumberFormat createFormat() {
    if (formatPattern != null) {
        DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();

        if (!StringUtils.isBlank(decimalSeparator))
            formatSymbols.setDecimalSeparator(decimalSeparator.charAt(0));

        if (!StringUtils.isBlank(groupingSeparator))
            formatSymbols.setGroupingSeparator(groupingSeparator.charAt(0));

        return new DecimalFormat(formatPattern, formatSymbols);
    } else {
        return NumberFormat.getNumberInstance();
    }
}

From source file:com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype.java

protected java.text.NumberFormat createLocalizedFormat(Locale locale) {
    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return createFormat();
    }//from   ww  w.  j  av a2s .c o m

    DecimalFormatSymbols formatSymbols = formatStrings.getFormatSymbols();
    if (!decimalSeparator.equals("")) {
        formatSymbols.setDecimalSeparator(decimalSeparator.charAt(0));
    }
    if (!groupingSeparator.equals("")) {
        formatSymbols.setGroupingSeparator(groupingSeparator.charAt(0));
    }

    DecimalFormat format = new DecimalFormat(formatPattern, formatSymbols);
    setupFormat(format);
    return format;
}

From source file:com.haulmont.chile.core.datatypes.impl.IntegerDatatype.java

@Override
public Integer parse(String value, Locale locale) throws ParseException {
    if (StringUtils.isBlank(value))
        return null;

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null)
        return parse(value);

    DecimalFormatSymbols formatSymbols = formatStrings.getFormatSymbols();
    NumberFormat format = new DecimalFormat(formatStrings.getIntegerFormat(), formatSymbols);
    return parse(value, format).intValue();
}

From source file:com.haulmont.chile.core.datatypes.impl.DoubleDatatype.java

@Override
public Double parse(String value, Locale locale) throws ParseException {
    if (StringUtils.isBlank(value)) {
        return null;
    }/*w w  w  .j a  v  a2 s . c  om*/

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return parse(value);
    }

    DecimalFormatSymbols formatSymbols = formatStrings.getFormatSymbols();
    NumberFormat format = new DecimalFormat(formatStrings.getDoubleFormat(), formatSymbols);
    return parse(value, format).doubleValue();
}

From source file:com.haulmont.chile.core.datatypes.impl.LongDatatype.java

@Override
public Long parse(String value, Locale locale) throws ParseException {
    if (StringUtils.isBlank(value)) {
        return null;
    }/*from w w w  .  j  a  v  a  2  s .c  o  m*/

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return parse(value);
    }

    DecimalFormatSymbols formatSymbols = formatStrings.getFormatSymbols();
    NumberFormat format = new DecimalFormat(formatStrings.getIntegerFormat(), formatSymbols);

    return parse(value, format).longValue();
}