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) 

Source Link

Document

Creates a DecimalFormat using the given pattern and the symbols for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

/**
 * Process inch value.// ww  w  .ja  v  a2 s .  c o m
 */
public static String toInch(double size) {
    //Rounding-off method. Remain 2 decimal number.
    DecimalFormat decimalFormat = new DecimalFormat("#0.00\"");

    return decimalFormat.format(size);
}

From source file:Main.java

public static String formatToStringWithoutDecimal(double value) {
    NumberFormat formatter = new DecimalFormat(FORMAT_NO_DECIMAL);
    return formatter.format(value);
}

From source file:Main.java

protected static final String encodingUTF8andIndex(byte[] src, int srart, int length) throws Exception {
    byte[] b = new String(src, srart, length, "euc-kr").getBytes("utf-8");
    String str = new String(b).trim();
    return new DecimalFormat("###,###.00").format(Double.valueOf(str));
}

From source file:Main.java

public static String formatString(Object text) {
    DecimalFormat df = new DecimalFormat("0.##");
    String str = df.format(text);
    return str;//  w ww. ja  va 2 s.co m
}

From source file:Main.java

public static String formatPrice(String price) {
    String tempPrice = "0";
    if (null != price && !"".equals(price)) {
        double d = Double.parseDouble(price);
        if (d != 0) {
            DecimalFormat df = new DecimalFormat("##0.00");
            tempPrice = df.format(Double.parseDouble(price));
        }/*from  ww  w  .  j a v  a 2s  .  co  m*/
    }
    return tempPrice;
}

From source file:Main.java

/**
 * Format distance to be displayed to the user
 *
 * @param d , distance in metres//w  w w  .  ja v  a 2s  .  c  o  m
 *
 * @return formatted {@link String}
 */
public static String formatDistance(double d) {
    DecimalFormat formatter = new DecimalFormat("#,### 'm'");
    d = Math.round(d);
    if (d > 1000) {
        formatter = new DecimalFormat("#,###.# 'km'");
        d = d / 1000;
    }

    return formatter.format(d);
}

From source file:Main.java

/**
 * Number format./*from   ww w.  j a  v a 2s. c  o  m*/
 * 
 * @param v
 *            the v
 * @return the string
 */
public static String numberFormat(double v) {
    if (v == 0.0) {
        return "0.00";
    }
    DecimalFormat nf = new DecimalFormat("#.##");
    nf.applyPattern("0.00");
    return nf.format(v);
}

From source file:Main.java

/**
 * Format the int number as your want//from   w w  w. j ava  2s.co m
 *
 * @param num
 * @param formatAs
 * @return
 * @throws Exception
 */
public static String formatNumber(int num, String formatAs) throws Exception {
    DecimalFormat df = new DecimalFormat(formatAs);
    String str2 = df.format(num);
    return str2;
}

From source file:Util.java

public static String getFilesystemPathFromDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*  ww w . j  ava 2s  . co  m*/
    NumberFormat format = new DecimalFormat("00");
    return File.separator + format.format(calendar.get(Calendar.YEAR)) + File.separator
            + format.format((calendar.get(Calendar.MONTH) + 1)) + File.separator
            + format.format(calendar.get(Calendar.DAY_OF_MONTH)) + File.separator;
}

From source file:Main.java

public static String readableFileSize(long size) {
    if (size <= 0)
        return "0";
    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
    return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}