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 getFriendlyFileSize(long fileSize) {
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    if (fileSize < 1024) {
        fileSizeString = df.format((double) fileSize) + "B";
    } else if (fileSize < 1048576) {
        fileSizeString = df.format((double) fileSize / 1024) + "KB";
    } else if (fileSize < 1073741824) {
        fileSizeString = df.format((double) fileSize / 1048576) + "MB";
    } else {// ww  w.  jav  a2  s. c o m
        fileSizeString = df.format((double) fileSize / 1073741824) + "GB";
    }
    return fileSizeString;
}

From source file:Main.java

public static String IndianFormat(BigDecimal n) {
    DecimalFormat formatter = new DecimalFormat("#,###.00");
    //we never reach double digit grouping so return
    if (n.doubleValue() < 100000) {
        return formatter.format(n.setScale(2, 1).doubleValue());
    }/*from   w w  w  . j  ava2s .c om*/
    StringBuffer returnValue = new StringBuffer();
    //Spliting integer part and decimal part
    String value = n.setScale(2, 1).toString();
    String intpart = value.substring(0, value.indexOf("."));
    String decimalpart = value.substring(value.indexOf("."), value.length());
    //switch to double digit grouping
    formatter.applyPattern("#,##");
    returnValue.append(formatter.format(new BigDecimal(intpart).doubleValue() / 1000)).append(",");
    //appending last 3 digits and decimal part
    returnValue.append(intpart.substring(intpart.length() - 3, intpart.length())).append(decimalpart);
    //returning complete string
    if (returnValue.toString().equals(".00")) {
        return "0.00";
    }

    return returnValue.toString();

}

From source file:Main.java

public static String convertToDecimal(String val) {

    String convertedValue = "";

    try {//from w ww .  ja v a 2s.  c o  m
        DecimalFormat df = new DecimalFormat("0.00");
        convertedValue = df.format(Double.parseDouble(val));
        df.setMaximumFractionDigits(2);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }

    return convertedValue;
}

From source file:Main.java

public static String convertToCurrency(float value) {
    DecimalFormat format = new DecimalFormat("#.##");
    try {//from  www  . jav  a2 s .c om
        value = Float.parseFloat(format.format(value));
    } catch (Exception e) {
        e.printStackTrace();
    }
    NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
    return defaultFormat.format(value);
}

From source file:Main.java

public static String getTwoPointString(double d) {
    if (d == 0)/*from w ww .j a v  a2 s  . c om*/
        return "0.00";
    try {
        DecimalFormat df = new DecimalFormat("######0.00");
        String text = df.format(d);
        return text;
    } catch (Exception e) {
        return "0.00";
    }
}

From source file:Main.java

public static String getFormattedAmount(double amount) {
    try {/*w  w  w.jav  a 2s  .  co m*/
        String amountString = new DecimalFormat("#,###").format(amount);
        return amountString.replace(",", ".");
    } catch (NumberFormatException e) {
        return "" + amount;
    } catch (NullPointerException e) {
        return "" + amount;
    }
}

From source file:Main.java

public static String getOnePointDouble(double d) {
    if (d == 0)/* w w  w.  ja  v  a2s .c  o  m*/
        return "0.0";
    try {
        DecimalFormat df = new DecimalFormat("######0.0");
        String text = df.format(d);
        if (text.endsWith("0"))
            return text.substring(0, text.length() - 1);
        return text;
    } catch (Exception e) {
        return "0.0";
    }
}

From source file:Main.java

public static double getProgress(int progress) {
    double temp = progress * 1.0;
    double pro = temp / 1024 / 1024;
    DecimalFormat df = new DecimalFormat("######0.00");
    return Double.parseDouble(df.format(pro));
}

From source file:Main.java

public static String formatToYuanYXS(String money) {
    DecimalFormat df = new DecimalFormat("#0.0");
    return df.format(formatToYuan(money));
}

From source file:Main.java

public static String formatDouble(Double number) {
    //#0.00 --> 123.4567 = 123.45 | 0.123456 = 0.12
    return new DecimalFormat("#0.0").format(number);
}