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

public static String Fiat2BTC(String fiat) {
    double val = 0.0;

    try {/*from   w w w  .j  a v  a 2 s  .c om*/
        val = Double.parseDouble(fiat);
    } catch (NumberFormatException nfe) {
        val = 0.0;
    }

    DecimalFormat df = new DecimalFormat("####0.0000");
    return df.format(Fiat2BTC(val));
}

From source file:Main.java

public static String formatMoneyYuan(String amount) {
    if (TextUtils.isEmpty(amount)) {
        return "0.00";
    }//from www  . j  a va2s  .c o m
    String result;
    try {
        double num = Double.parseDouble(amount);
        DecimalFormat formater = new DecimalFormat("###,##0.00");
        result = formater.format(num);
    } catch (NumberFormatException e) {
        result = amount;
    }
    return result;
}

From source file:Main.java

public static String formatWithDecimal(double price) {
    NumberFormat formatter = new DecimalFormat(FORMAT_WITH_DECIMAL);
    return formatter.format(price);
}

From source file:Main.java

public static String removeZero(String s) {
    if (!TextUtils.isEmpty(s)) {
        try {/*from  w  w w .j  av  a  2s.  c o  m*/

            double d = Double.parseDouble(s);
            DecimalFormat format = new DecimalFormat("0.#");
            return format.format(d);
        } catch (Exception e) {
            return "0";
        }
    } else {
        return "0";
    }

}

From source file:Main.java

/**
 * yyyyMMddHHmmss/* w  w  w. ja v a 2  s .  co  m*/
 */
public static String getFormatTime3(Calendar c) {
    DecimalFormat df = new DecimalFormat("00");
    String strFileName = c.get(Calendar.YEAR) + df.format((c.get(Calendar.MONTH) + 1))
            + df.format(c.get(Calendar.DAY_OF_MONTH)) + df.format(c.get(Calendar.HOUR_OF_DAY))
            + df.format(c.get(Calendar.MINUTE)) + df.format(c.get(Calendar.SECOND));

    return strFileName;
}

From source file:Main.java

public static String getFileSize(long size) {
    if (size <= 0) {
        return "0";
    }/*w  w w .j av  a  2  s.com*/
    java.text.DecimalFormat df = new DecimalFormat("##.##");
    float temp = (float) size / 1024;
    if (temp >= 1024) {
        return df.format(temp / 1024) + "M";
    } else {
        return df.format(temp) + "K";
    }
}

From source file:Main.java

/**
 * Str format.//  w w  w .  j a v  a2 s .com
 * 
 * @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 getFileSizeDescription(Context context, long bytes) {
    String value = "";
    if (bytes < 1000) {
        value = (int) bytes + "B";
    } else if (bytes < 1000000) {
        value = Math.round(bytes / 1000.0) + "K";
    } else if (bytes < 1000000000) {
        DecimalFormat df = new DecimalFormat("#0.0");
        value = df.format(bytes / 1000000.0) + "M";
    } else {/*from ww  w  .  j  a va 2s  .c  o  m*/
        DecimalFormat df = new DecimalFormat("#0.00");
        value = df.format(bytes / 1000000000.0) + "G";
    }
    return value;
}

From source file:Main.java

/**
 * yyyy-MM-dd HH:mm:ss/* ww  w  .java 2s  .  c  om*/
 */
public static String getFormatTime1(Calendar c) {
    if (null == c) {
        return "null";
    }
    DecimalFormat df = new DecimalFormat("00");
    String strCurrTime = c.get(Calendar.YEAR) + "-" + df.format((c.get(Calendar.MONTH) + 1)) + "-"
            + df.format(c.get(Calendar.DAY_OF_MONTH)) + " " + df.format(c.get(Calendar.HOUR_OF_DAY)) + ":"
            + df.format(c.get(Calendar.MINUTE)) + ":" + df.format(c.get(Calendar.SECOND));

    return strCurrTime;
}

From source file:Main.java

/**
 * Convert a number of meters to feet./*from  w  w w. j a  v a 2  s .co  m*/
 * @param meters Value to convert in meters.
 * @return meters converted to feet.
 */
public static double asFeet(final double meters) {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(meters * FEET_CONVERT));
}