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:com.wuliu.biz.util.WuliuWholeOrderDetailUtil.java

public static WuliuWholeOrderDetailModel builduliuWholeDetailModel(
        WuliuOrderDetailModel wuliuOrderDetailModel) {
    WuliuWholeOrderDetailModel ret = new WuliuWholeOrderDetailModel();
    ret.setCount(wuliuOrderDetailModel.getCount());
    ret.setHeight(wuliuOrderDetailModel.getHeight());
    ret.setId(wuliuOrderDetailModel.getId());
    ret.setLength(wuliuOrderDetailModel.getLength());
    ret.setMainOrderId(wuliuOrderDetailModel.getMainOrderId());
    ret.setStatus(wuliuOrderDetailModel.getStatus());
    ret.setWeight(wuliuOrderDetailModel.getWeight());
    ret.setWidth(wuliuOrderDetailModel.getWidth());
    ret.setTotalVolumn(wuliuOrderDetailModel.getHeight() * wuliuOrderDetailModel.getLength()
            * wuliuOrderDetailModel.getWidth() * wuliuOrderDetailModel.getCount());
    ret.setTotalWeight(wuliuOrderDetailModel.getWeight() * wuliuOrderDetailModel.getCount());

    DecimalFormat df = new DecimalFormat("0.#");
    if (ret.getLength() != null) {
        ret.setLengthForDisplay(df.format(ret.getLength() / 10.0));
    }//from   ww  w .  j  a  va  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));
    }

    df.applyPattern("0.###");
    if (ret.getTotalVolumn() != null) {
        if (ret.getTotalVolumn() % 1000000 == 0) {
            ret.setTotalVolumnForDisplay(df.format((ret.getTotalVolumn() / 1000000) / 1000.0));
        } else {
            ret.setTotalVolumnForDisplay(df.format(Math.ceil(ret.getTotalVolumn() / 1000000.0f) / 1000.0));
        }
    }

    df.applyPattern("0.#");
    if (ret.getTotalWeight() != null) {
        if (ret.getTotalWeight() % 1000 == 0) {
            ret.setTotalWeightForDisplay(String.valueOf(ret.getTotalWeight() / 1000));
        } else {
            ret.setTotalWeightForDisplay(df.format((ret.getTotalWeight() / 1000.0)));
        }
    }

    return ret;
}

From source file:Main.java

public static String kgToLB_ForBodyScale(float paramFloat) {
    int i = (int) Math.floor(1.0F + (32768.0F + 144479.0F * (paramFloat * 10.0F)) / 65536.0F);
    if (i % 2 != 0) {
        i--;/*www  .j a  va  2 s .  co  m*/
    }
    DecimalFormat localDecimalFormat = new DecimalFormat();
    localDecimalFormat.setMaximumFractionDigits(1);
    localDecimalFormat.setGroupingSize(0);
    localDecimalFormat.setRoundingMode(RoundingMode.FLOOR);
    return localDecimalFormat.format(0.1D * i);
}

From source file:Main.java

public static String getMemoryData(long total) {

    String dataText = "0";
    DecimalFormat df = new DecimalFormat("###.00");
    if (total == -1) {
        return dataText;
    } else if (total < 1024) {
        dataText = "<1MB";

    } else if (total < 1024 * 1024) {
        dataText = df.format(total / 1024f) + "MB";
    }/*from w  w w  .j av a2 s  .  c o m*/
    return dataText;
}

From source file:Main.java

/**
 * Convert time to a string//from ww w  .  j a v  a2  s .c o  m
 *
 * @param millis e.g.time/length from file
 * @return Formatted string (hh:)mm:ss
 */
public static String millisToString(long millis) {
    boolean negative = millis < 0;
    millis = Math.abs(millis);

    millis /= 1000;
    int sec = (int) (millis % 60);
    millis /= 60;
    int min = (int) (millis % 60);
    millis /= 60;
    int hours = (int) millis;

    String time;
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    format.applyPattern("00");
    if (millis > 0) {
        time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
    } else {
        time = (negative ? "-" : "") + min + ":" + format.format(sec);
    }

    return time;
}

From source file:Main.java

public static String kgToLBoz(float paramFloat) {
    float f1 = (32768.0F + 23117.0F * paramFloat) / 65536.0F;
    int i = (int) (f1 * 0.1F) / 16;
    float f2 = f1 * 0.1F % 16.0F;
    DecimalFormat localDecimalFormat = new DecimalFormat();
    localDecimalFormat.setMaximumFractionDigits(1);
    localDecimalFormat.setGroupingSize(0);
    localDecimalFormat.setRoundingMode(RoundingMode.FLOOR);
    return i + ":" + localDecimalFormat.format(f2);
}

From source file:Main.java

public static String getTrafficData(long total) {

    String dataText = "0";
    DecimalFormat df = new DecimalFormat("###.00");
    if (total == -1) {
        return dataText;
    } else if (total < 10240) {
        dataText = "<10K";

    } else if (total < 1024 * 1024) {
        dataText = df.format(total / 1024f) + "KB";
    } else if (total < 1024 * 1024 * 1024) {
        dataText = df.format(total / 1024f / 1024f) + "MB";
    } else if (total < 1024 * 1024 * 1024 * 1024) {
        dataText = df.format(total / 1024f / 1024f / 1024f) + "GB";
    }//from   w ww. j a  v a 2s . co m
    return dataText;
}

From source file:Main.java

public static String roundWithTwoDecimals(double number) {
    try {//  w  w w .java2 s .c  om
        // format based on default locale
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        df.setDecimalFormatSymbols(dfs);
        df.applyPattern("#####0.00");
        return df.format(number);
    } catch (IllegalArgumentException e) {
        return null;
    }
}

From source file:org.openfaces.component.chart.impl.helpers.ChartInfoUtil.java

public static PieSectorInfo getPieSectorInfo(PieDataset pieDataset, Comparable comparable, int dsIndex) {
    double total = 0;
    List keys = pieDataset.getKeys();
    for (Object key : keys) {
        Object value = pieDataset.getValue((Comparable) key);
        if (value != null) {
            double dValue = ((Number) value).doubleValue();
            if (dValue > 0)
                total = total + dValue;/*from  w ww .  ja v a  2  s .  c o m*/
        }
    }
    int index = pieDataset.getIndex(comparable);

    Object value = pieDataset.getValue(index);
    double dValue = 0;
    if (value != null) {
        dValue = ((Number) value).doubleValue();
    }

    PieSectorInfo sector = new PieSectorInfoImpl();
    sector.setKey(comparable);
    sector.setValue(value);
    sector.setSeriesTotal(total);
    sector.setIndex(index);
    sector.setSeries(new SeriesInfoImpl());

    sector.getSeries().setIndex(dsIndex);

    double p = (dValue / total);
    DecimalFormat nf1 = new DecimalFormat("#.00%");
    String proportionalPercent = nf1.format(p);

    sector.setProportionalValue(proportionalPercent);
    return sector;
}

From source file:io.hops.hopsworks.common.util.FormatUtils.java

public static String storage(Long s) {
    DecimalFormat format = new DecimalFormat("#.#");
    Double size = (double) s;
    if (size < K) {
        return format.format(size) + " B";
    }//from   w  w w. ja  v  a 2  s  .  c  o m
    if (size < M) {
        return format.format(size / K) + " KB";
    }
    if (size < G) {
        return format.format(size / M) + " MB";
    }
    if (size < T) {
        return format.format(size / G) + " GB";
    }
    return format.format(size / T) + " TB";
}

From source file:io.hops.hopsworks.common.util.FormatUtils.java

public static String time(Long t) {
    DecimalFormat format = new DecimalFormat("#.#");
    Double time = (double) t / 1000d;
    if (time < m) {
        return format.format(time) + "s";
    }//from ww w  . j  av  a  2s. c om
    if (time < h) {
        return format.format(time / m) + "m";
    }
    if (time < d) {
        return format.format(time / h) + "h";
    }
    return format.format(time / d) + "d";
}