Example usage for java.text DecimalFormat format

List of usage examples for java.text DecimalFormat format

Introduction

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

Prototype

public final String format(double number) 

Source Link

Document

Specialization of format.

Usage

From source file:de.micromata.genome.gwiki.plugin.rogmp3_1_0.Mp3Stats.java

public static void genStatistics(Mp3Db db, XmlElement node) {
    DecimalFormat df = new DecimalFormat("#,###");
    node.add(Html.p(code("Komponisten: " + df.format(db.composers.table.size()))));
    List<Media> ml = db.getMedia();
    int cdc = 0;// w w w  .ja v a 2  s.co  m
    for (Media m : ml) {
        String mc = m.get(Media.MEDIACOUNT);
        if (StringUtils.isBlank(mc) == true) {
            ++cdc;
            continue;
        }
        try {
            cdc += NumberUtils.createInteger(mc);
        } catch (NumberFormatException ex) {
            ++cdc;
        }
    }
    int tcest = 36000;
    node.add(Html.p(code("Medien: " + df.format(ml.size()) + ", single: " + df.format(cdc))));
    node.add(Html.p(code("Titel: " + df.format(db.title.table.size()))));
    node.add(Html.p(code("<i>Only Parts with trackinfo</i>:")));
    double factor = (double) tcest / (double) db.tracks.table.size();

    node.add(Html.p(code("Tracks: " + df.format(db.tracks.table.size()))));
    long timesec = 0;
    long sizebytes = 0;
    for (String[] td : db.tracks.table) {
        Track track = new Track(db, td);
        timesec += track.getTime();
        sizebytes += track.getSize();
    }
    long esttimesec = (long) (factor * timesec);
    SimpleDateFormat sd = new SimpleDateFormat("MM-dd HH:mm:ss");
    String dur = sd.format(new Date(timesec * 1000));
    String esdur = sd.format(new Date(esttimesec * 1000));
    String size = df.format(sizebytes);

    node.add(Html.p(code("Total time: " + dur + ", " + df.format((int) (timesec / 3600)) + " hours")));
    node.add(Html.p(code("Total size: " + size + " (Parts)")));

    node.add(Html.p(code("Orchester: " + df.format(db.orchesters.table.size()))));
    node.add(Html.p(code("Intepreten: " + df.format(db.interprets.table.size()))));
}

From source file:org.esupportail.papercut.domain.UserPapercutInfos.java

/**
 * Papercut WebService gives balance values like 1.9465000000000003 
 * Error of epsilon machine is suspected in Papercut !
 * -> to workaround, we simply here round the balance ... 
 * //from  w  w w.  j ava 2  s. c o m
 * Note that 5 decimals is the max in papercut today, but maybe one day it will be more so here we return with 10 max 
 * 
 * @param balance
 * @return
 */
private static String fixRoundPapercutError(String balance) {
    DecimalFormat df = new DecimalFormat("#.##########");
    df.setRoundingMode(RoundingMode.HALF_UP);
    return df.format(Double.valueOf(balance));
}

From source file:Main.java

public static String IndianFormat(BigDecimal n) {
    DecimalFormat formatter = new DecimalFormat("#,###.00");
    //we never reach double digit grouping so return
    if (n.doubleValue() < 100000) {
        return formatter.format(n.setScale(2, 1).doubleValue());
    }/*  w w w.  j ava  2  s  .c  o m*/
    StringBuffer returnValue = new StringBuffer();
    //Spliting integer part and decimal part
    String value = n.setScale(2, 1).toString();
    String intpart = value.substring(0, value.indexOf("."));
    String decimalpart = value.substring(value.indexOf("."), value.length());
    //switch to double digit grouping
    formatter.applyPattern("#,##");
    returnValue.append(formatter.format(new BigDecimal(intpart).doubleValue() / 1000)).append(",");
    //appending last 3 digits and decimal part
    returnValue.append(intpart.substring(intpart.length() - 3, intpart.length())).append(decimalpart);
    //returning complete string
    if (returnValue.toString().equals(".00")) {
        return "0.00";
    }

    return returnValue.toString();

}

From source file:Main.java

public static String formatPercentWithFixedFractionDigit(BigDecimal number, int fractionDigit) {
    String result = "0";

    if (number != null) {
        number = number.multiply(new BigDecimal("100"));
        DecimalFormat formatter = new DecimalFormat();
        formatter.setMaximumFractionDigits(fractionDigit);
        formatter.setMinimumFractionDigits(fractionDigit);

        result = formatter.format(number);
    }//from ww  w.j  a  va2s .  co  m
    return result;
}

From source file:com.fengduo.bee.commons.util.NumberParser.java

/**
 * ?/*from   w ww .j  a  v a 2  s  .co  m*/
 * 
 * @param number
 * @param precision ???
 * @return
 */
public static String format2Str(double number, int precision) {
    String pattern = "0.";
    for (int i = 0; i < precision; i++) {
        pattern += "0";
    }
    DecimalFormat dg = new DecimalFormat(pattern); // ???
    return dg.format(number);
}

From source file:Main.java

public static String setFileSize(long size) {
    DecimalFormat df = new DecimalFormat("###.##");
    float f = ((float) size / (float) (1024 * 1024));

    if (f < 1.0) {
        float f2 = ((float) size / (float) (1024));

        return df.format(new Float(f2).doubleValue()) + "KB";

    } else {/*ww  w.  j  a v  a 2 s  .  c  o  m*/
        return df.format(new Float(f).doubleValue()) + "M";
    }
}

From source file:com.wuliu.biz.util.WuliuOrderDetailUtil.java

public static WuliuOrderDetailModel convertToWuliuOrderDetailModel(WuliuOrderDetailDO wuliuOrderDetailDO) {

    WuliuOrderDetailModel ret = new WuliuOrderDetailModel();
    ret.setCount(wuliuOrderDetailDO.getCount());
    ret.setHeight(wuliuOrderDetailDO.getHeight());
    ret.setId(wuliuOrderDetailDO.getId());
    ret.setLength(wuliuOrderDetailDO.getLength());
    ret.setMainOrderId(wuliuOrderDetailDO.getMainOrderId());
    ret.setStatus(wuliuOrderDetailDO.getStatus());
    ret.setWidth(wuliuOrderDetailDO.getWidth());
    ret.setWeight(wuliuOrderDetailDO.getWeight());
    ret.setStatus(wuliuOrderDetailDO.getStatus());

    DecimalFormat df = new DecimalFormat("0.#");
    if (ret.getLength() != null) {
        ret.setLengthForDisplay(df.format(ret.getLength() / 10.0));
    }//from   w w w  . j  av  a  2  s . co m

    if (ret.getWidth() != null) {
        ret.setWidthForDisplay(df.format(ret.getWidth() / 10.0));
    }

    if (ret.getHeight() != null) {
        ret.setHeightForDisplay(df.format(ret.getHeight() / 10.0));
    }

    if (ret.getWeight() != null) {
        ret.setWeightForDisplay(df.format(ret.getWeight() / 1000.0));
    }

    return ret;
}

From source file:com.wuliu.biz.util.WuliuWholeOrderUtil.java

public static WuliuWholeOrderModel buildWholeModel(WuliuOrderModel wuliuOrderModel,
        List<WuliuOrderDetailModel> wuliuOrderDetailModels) {
    WuliuWholeOrderModel ret = new WuliuWholeOrderModel();
    ret.setCarIndex(wuliuOrderModel.getCarIndex());
    ret.setDaishouFee(wuliuOrderModel.getDaishouFee());
    ret.setId(wuliuOrderModel.getId());/*from  w w  w.j ava2s  .  c o m*/
    ret.setJiashouFee(wuliuOrderModel.getJiashouFee());
    ret.setMemberId(wuliuOrderModel.getMemberId());
    ret.setOrderDate(wuliuOrderModel.getOrderDate());
    ret.setOrderNumber(wuliuOrderModel.getOrderNumber());
    ret.setStatus(wuliuOrderModel.getStatus());
    ret.setId(wuliuOrderModel.getId());
    ret.setWuliuOrderDetailModels(
            WuliuWholeOrderDetailUtil.builduliuWholeDetailModelList(wuliuOrderDetailModels));
    ret.setZhongzhuanFee(wuliuOrderModel.getZhongzhuanFee());
    ret.setComments(wuliuOrderModel.getComments());

    DecimalFormat df = new DecimalFormat("0.##");
    if (ret.getZhongzhuanFee() != null) {
        ret.setZhongzhuanFeeForDisplay(df.format(ret.getZhongzhuanFee() / 100.0));
    }

    if (ret.getDaishouFee() != null) {
        ret.setDaishouFeeForDisplay(df.format(ret.getDaishouFee() / 100.0));
    }

    if (ret.getJiashouFee() != null) {
        ret.setJiashouFeeForDisplay(df.format(ret.getJiashouFee() / 100.0));
    }

    int count = 0;
    if (CollectionUtils.isNotEmpty(wuliuOrderDetailModels)) {
        for (WuliuOrderDetailModel item : wuliuOrderDetailModels) {
            count += item.getCount();
        }
    }

    ret.setOrderNumber(wuliuOrderModel.getOrderNumber());
    return ret;
}

From source file:e3fraud.gui.GraphingTool.java

public static JFreeChart generateGraph(E3Model model, Resource need, int startValue, int endValue,
        boolean expected) {
    //Get list of actors
    Set<Resource> actors = model.getActors();
    //generate a series
    Map<Resource, XYSeries> actorSeriesMap = model.getTotalForActors(need, startValue, endValue, expected);

    //for each actor
    XYSeriesCollection line_chart_dataset = new XYSeriesCollection();

    for (Resource actor : actors) {
        //add it's series to the chart
        XYSeries series = actorSeriesMap.get(actor);
        line_chart_dataset.addSeries(series);
        double slope;
        if (series.getItemCount() > 1) {
            slope = (series.getY(0).doubleValue() - series.getY(1).doubleValue())
                    / (series.getX(0).doubleValue() - series.getX(1).doubleValue());
        } else {/*from  www.j a  v  a 2 s  .com*/
            slope = 0;
        }
        DecimalFormat df = new DecimalFormat("#.##");
        series.setKey(series.getKey() + "\nAvg.\t = \t" + df.format(model.getLastKnownAverages().get(actor))
                + "\nSlope\t = \t" + df.format(slope));
    }

    /* Step -2:Define the JFreeChart object to create line chart */
    JFreeChart lineChartObject;
    if (expected) {
        lineChartObject = ChartFactory.createScatterPlot(
                "(Ideal) Profit Vs Occurences of \"" + need.getProperty(E3value.e3_has_name).getString()
                        + " \"",
                "Occurences of \"" + need.getProperty(E3value.e3_has_name).getString() + " \"",
                "Profit (in Euro)", line_chart_dataset, PlotOrientation.VERTICAL, true, true, false);
    } else {
        lineChartObject = ChartFactory.createScatterPlot(
                "(Non-ideal) Profit Vs Occurences of \"" + need.getProperty(E3value.e3_has_name).getString()
                        + " \"",
                "Occurences of \"" + need.getProperty(E3value.e3_has_name).getString() + " \"",
                "Profit (in Euro)", line_chart_dataset, PlotOrientation.VERTICAL, true, true, false);
    }
    return lineChartObject;
}

From source file:Main.java

public static String formatNumber(int number) {
    DecimalFormat formatter = new DecimalFormat();
    DecimalFormatSymbols symbol = new DecimalFormatSymbols();
    symbol.setGroupingSeparator('.');
    formatter.setDecimalFormatSymbols(symbol);
    return formatter.format(number);
}