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 removeZero(String s) {
    if (!TextUtils.isEmpty(s)) {
        try {/*from ww w.j ava2 s .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:DecimalFormatDemo.java

static public void customFormat(String pattern, double value) {
    DecimalFormat myFormatter = new DecimalFormat(pattern);
    String output = myFormatter.format(value);
    System.out.println(value + "  " + pattern + "  " + output);
}

From source file:Main.java

public static String FourDigits(int d) {
    DecimalFormat fourDecimal = new DecimalFormat("0000");
    return String.valueOf(fourDecimal.format(d));
}

From source file:Main.java

public static String formatDouble(double d) {
    DecimalFormat formatter = new DecimalFormat("#,###.##");
    d = Math.round(d);/*  w  w  w  .  ja va  2 s .c  om*/
    return formatter.format(d);
}

From source file:Main.java

public static String getDecimal(double decimal, int length) {
    String strLen = ".";
    if (length <= 0) {
        strLen = "";
    } else {//ww w  . j a  v a  2  s .  c o  m
        for (int i = 0; i < length; i++) {
            strLen += "0";
        }
    }
    java.text.DecimalFormat df = new java.text.DecimalFormat("0" + strLen);
    return df.format(decimal);
}

From source file:Main.java

/**
 * Rounds the given results to two decimal places.
 * //from w w w. j  av  a 2  s .  c o  m
 * @param results
 */
private static void roundResults(double[] results) {
    for (int i = 0; i < results.length; i++) {
        double result = results[i];
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        results[i] = Double.valueOf(twoDForm.format(result));
    }
}

From source file:Main.java

public static String formatMoneyYuan(String amount) {
    if (TextUtils.isEmpty(amount)) {
        return "0.00";
    }//from w  w  w  .  j a va2 s .  co  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 numberFormat(Double number) {
    DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
    return format.format(number);
}

From source file:Main.java

public static String lbToKg(float paramFloat) {
    float f = (float) (0.45359D * paramFloat);
    DecimalFormat localDecimalFormat = new DecimalFormat("######.00");
    Log.v("tag", "string2:" + localDecimalFormat.format(f));
    return localDecimalFormat.format(f);
}

From source file:Main.java

public static String formatMoney(String amount) {
    if (TextUtils.isEmpty(amount)) {
        return "0.00";
    }/*from w w  w  .j a  va 2s.  c  o m*/
    String result;
    try {
        double num = Double.parseDouble(amount);
        DecimalFormat formater = new DecimalFormat("###,##0.00");
        result = formater.format(num / 100.0d);
    } catch (NumberFormatException e) {
        result = amount;
    }
    return result;
}