Example usage for java.text DecimalFormat applyPattern

List of usage examples for java.text DecimalFormat applyPattern

Introduction

In this page you can find the example usage for java.text DecimalFormat applyPattern.

Prototype

public void applyPattern(String pattern) 

Source Link

Document

Apply the given pattern to this Format object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.applyPattern("##.##");
    System.out.println(format.format(-123456789.12345678));

}

From source file:MainClass.java

public static void main(String[] argv) {
    double d = 1234567.89;
    double n = -1234567.89;

    String pattern = "#,###.##;(#,###.##)";

    NumberFormat nf = NumberFormat.getInstance();
    if (nf instanceof DecimalFormat) {
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern(pattern);
        System.out.println(df.format(d));
        System.out.println(df.format(n));
    }//from ww  w  .jav a  2s .  co m

}

From source file:MainClass.java

public static void main(String[] argv) {
    NumberFormat nf = NumberFormat.getInstance();
    if (nf instanceof DecimalFormat) {
        DecimalFormat df = (DecimalFormat) nf;

        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();

        dfs.setCurrencySymbol("AD$");

        df.setDecimalFormatSymbols(dfs);

        df.applyPattern("\u00a4#,###.##");
    }/*from   www  . j a  v  a 2s. c  o  m*/

    System.out.println(nf.format(1234.56));

}

From source file:Main.java

public static String getVersionString(final double price) {
    final NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
    final DecimalFormat df = (DecimalFormat) nf;
    df.applyPattern("###,###.####");
    final String output = df.format(price);
    return output;
}

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);
    String output = df.format(value);
    System.out.println(pattern + "  " + output + "  " + loc.toString());
}

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

/**
 * Number format.//from w  w  w . j a  va2s  .  co  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 float formatFloat(float value) {
    DecimalFormat df = new DecimalFormat();
    String style = "0.0";
    df.applyPattern(style);
    return Float.valueOf(df.format(value));
}

From source file:Main.java

public static String changeMoney(String pattern, String value) {
    double va = 0;
    try {//from www  . j a v a  2 s.  c o  m
        va = Double.parseDouble(value);
    } catch (Exception e) {
        return "";
    }
    DecimalFormat myFormatter = new DecimalFormat(pattern);
    myFormatter.applyPattern(pattern);
    return myFormatter.format(va);
}

From source file:Main.java

/**
 * Str format./*from  ww w.ja  v  a  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);
}