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 double roundTwoDecimals(double d) {
    try {//from  w  ww  .  ja  v  a2 s . co m
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    } catch (NumberFormatException nfe) {
        Log.d("nfe", nfe.toString());
        try {
            int i = (int) (d * 100);
            d = (i / 100);
            return d;
        } catch (NumberFormatException ne) {
            return d;
        }
    }
}

From source file:Main.java

public static double roundTwoDecimals(double d) {
    // try {//from   ww  w .j  a  va 2s  . c o m
    //DecimalFormat twoDForm = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance(Locale.getDefault()));
    // NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    DecimalFormat dFormat = new DecimalFormat("####,###,###.#");

    return Double.valueOf(dFormat.format(d));
    /**}
    catch (Exception exx)
    {
    return  d;
    }**/
}

From source file:Main.java

/**
 * Number format.//www. j ava  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

public static String calculateSizeToString(long size) {
    String str = "";
    DecimalFormat df = new DecimalFormat("#.00");
    if (size < 1024) {
        str = df.format(size) + "B";
    } else if (size < 1024 * 1024) {
        str = df.format(size / 1024) + "KB";
    } else if (size < 1024 * 1024 * 1024) {
        str = df.format(size / 1024 / 1024) + "M";
    } else {/*  w  w  w. j  av  a  2s  .  c  o  m*/
        str = df.format(size / 1024 / 1024 / 1024) + "G";
    }
    return str;
}

From source file:Main.java

public static String formatMoney(double amount) {
    DecimalFormat df = new DecimalFormat();
    df.applyPattern("##,###0.00");
    return df.format(amount);
}

From source file:Main.java

public static String liangweixiaoshu2(String value) {
    DecimalFormat df = new DecimalFormat("#.00");
    Double dValue = Double.parseDouble(value);
    return df.format(dValue);
}

From source file:Main.java

public static String convertToCurrency(float value) {
    DecimalFormat format = new DecimalFormat("#.##");
    try {/*from  ww  w . j av  a  2 s  .c o m*/
        value = Float.parseFloat(format.format(value));
    } catch (Exception e) {
        e.printStackTrace();
    }
    NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
    return defaultFormat.format(value);
}

From source file:DecimalFormatDemo.java

static public void localizedFormat(String pattern, double value, Locale loc) {
    NumberFormat nf = NumberFormat.getNumberInstance(loc);
    DecimalFormat df = (DecimalFormat) nf;
    df.applyPattern(pattern);//from w  w  w. j  av  a  2 s .  c  o m
    String output = df.format(value);
    System.out.println(pattern + "  " + output + "  " + loc.toString());
}

From source file:Main.java

public static String formatToCurrency(double amount, String sign) {
    DecimalFormat df;
    df = new DecimalFormat((sign.length() > 0 ? sign + " " : "") + "#,##0.00");
    return df.format(amount);
}

From source file:Main.java

/**
 * yyyyMMddHHmmss/*from w ww .ja  v  a 2  s  .com*/
 */
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;
}