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:Main.java

public static String MybigdecimalToString(BigDecimal myBigD) {
    myBigD = myBigD.setScale(2);//w ww.  j  a v a  2  s .  com
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(0);
    return df.format(myBigD);
}

From source file:Main.java

/**
 * Returns a formatted string representation of the number of bytes specified. The largest
 * suitable suffix up until GB will be used, with the returned value expressed to two 
 * decimal places./*w  w w  . j a  v  a 2 s  .c o  m*/
 * @param bytes The number of bytes to be reported.
 * @return The specified number of bytes, formatted as a string, in bytes, KB, MB or GB.
 */
static public String formatBytes(long bytes) {

    double val = 0;
    String units = "";

    if (bytes < 1024) {
        val = bytes;
        units = "bytes";
    } else if (bytes < 1048576) {
        val = (bytes / 1024.0f);
        units = "KB";
    } else if (bytes < 1073741824) {
        val = (bytes / 1048576.0f);
        units = "MB";
    } else {
        val = (bytes / 1073741824.0f);
        units = "GB";
    }

    DecimalFormat df = new DecimalFormat("###.##");
    return df.format(val) + " " + units;

}

From source file:Main.java

public static double calculateMinWeight(double height) {
    DecimalFormat format = new DecimalFormat("0.0");
    double result = 18.5 * height * height / 10000;
    return Double.valueOf(format.format(result).toString());
}

From source file:Main.java

public static double calculateMaxWeight(double height) {
    DecimalFormat format = new DecimalFormat("0.0");
    double result = 24.0 * height * height / 10000;
    return Double.valueOf(format.format(result).toString());
}

From source file:Main.java

public static String formatNum(double value) {
    String retValue = null;/*from  www. j  a  v a  2s. co m*/
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(0);
    df.setMaximumFractionDigits(2);
    retValue = df.format(value);
    retValue = retValue.replaceAll(",", "");
    return retValue;
}

From source file:com.hangum.tadpole.commons.util.NumberFormatUtils.java

/**
 * , ??.// w ww . j a  v a 2 s.  co  m
 * @param value
 * @return
 */
public static String commaFormat(double value) {
    //      String tmpVal = String.format("%.2f", value);
    DecimalFormat df = new DecimalFormat("#,###.##");
    String tmpVal = df.format(value).toString();

    if (-1 == StringUtils.indexOf(tmpVal, ".00")) {
        return tmpVal;
    } else {
        return StringUtils.replaceOnce(tmpVal, ".00", "");
    }
}

From source file:Main.java

public static double calculateBMI(double weight, double height) {
    DecimalFormat format = new DecimalFormat("0.00");
    double result = weight / (height * height) * 10000;
    return Double.valueOf(format.format(result).toString());
}

From source file:Main.java

/**
 * Str format.// ww  w  .  j ava  2  s .  co  m
 * 
 * @param v
 *            the v
 * @return the string
 */
public static String strFormat(String v) {
    if (v == null) {
        return "0.00";
    }
    double dv = Double.parseDouble(v);
    DecimalFormat nf = new DecimalFormat("#.##");
    nf.applyPattern("0.00");
    return nf.format(dv);
}

From source file:Main.java

public static String getFileSize(long fileSize) {
    int freeUnit;
    for (freeUnit = 0; fileSize >= 100; freeUnit++) {
        fileSize /= 1024;/*from   w  w  w  .ja  v a  2 s .  c  o m*/
    }
    DecimalFormat decFormat = new DecimalFormat("0.0");
    String doubleString = decFormat.format(fileSize);
    StringBuffer buffer = new StringBuffer();
    buffer.append(doubleString);
    switch (freeUnit) {
    case 0:
        buffer.append("B");
        break;
    case 1:
        buffer.append("KB");
        break;
    case 2:
        buffer.append("MB");
        break;
    case 3:
        buffer.append("GB");
        break;
    case 4:
        buffer.append("TB");
        break;
    default:
        buffer.append("err");
        break;
    }
    return buffer.toString();
}

From source file:Main.java

public static double convertKMToMiles(String kilometers) {

    DecimalFormat decimalFormat = new DecimalFormat("#");
    int km = Integer.parseInt(kilometers);
    double miles = 0.621 * km;

    decimalFormat.format(miles);
    return Math.abs(miles);
}