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 byteToKB(int bt) {
    DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
    df.setMaximumFractionDigits(2);/*  w w  w  . j a v a2 s  .  c  o  m*/
    return df.format(bt / 1024.0) + " KB";
}

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:Main.java

public static String fileLength(long length) {
    String lenStr = null;//from  www . j  av  a  2  s.  c  om
    DecimalFormat formater = new DecimalFormat("#0.##");
    if (length < 1024) {
        lenStr = formater.format(length) + " Byte";
    } else if (length < 1024 * 1024) {
        lenStr = formater.format(length / 1024.0f) + " KB";
    } else if (length < 1024 * 1024 * 1024) {
        lenStr = formater.format(length / (1024 * 1024)) + " MB";
    } else {
        lenStr = formater.format(length / (1024 * 1024 * 1024)) + " GB";
    }
    return lenStr;
}

From source file:Main.java

/**
 * yyyy-MM-dd HH:mm:ss/*w w  w.  j a v a  2  s. co  m*/
 */
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

public static double getJulDate() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    double extra = (100.0 * year) + month - 190002.5;
    double julianDay = (367.0 * year) - (Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0))
            + Math.floor((275.0 * month) / 9.0) + day + ((hour + ((minute + (second / 60.0)) / 60.0)) / 24.0)
            + 1721013.5 - ((0.5 * extra) / Math.abs(extra)) + 0.5;
    DecimalFormat sixDigitFormat = new DecimalFormat("#.######");
    return Double.valueOf(sixDigitFormat.format(julianDay));
}

From source file:Main.java

public static float formatFloat(float value) {
    DecimalFormat df = new DecimalFormat();
    String style = "0.0";
    df.applyPattern(style);/*from  w  ww .  j av  a  2  s .c om*/
    return Float.valueOf(df.format(value));
}

From source file:Main.java

public static final String objectToString(Object obj, DecimalFormat fmt) {
    fmt.setDecimalSeparatorAlwaysShown(false);
    if (obj instanceof Double)
        return fmt.format(((Double) obj).doubleValue());
    if (obj instanceof Long)
        return fmt.format(((Long) obj).longValue());
    else//from  w w  w .  ja v a2s .  co m
        return obj.toString();
}

From source file:Main.java

public static String round(double value) {
    DecimalFormat decimalFormat = new DecimalFormat("0.00");
    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);

    return decimalFormat.format(value);
}

From source file:Main.java

public static Double toDecimal(Double value) {

    DecimalFormat df = new DecimalFormat("#.000000");
    df.setRoundingMode(RoundingMode.DOWN);

    return Double.parseDouble(df.format(value));
}

From source file:Main.java

public static String getDataSize(long var0) {
    DecimalFormat var2 = new DecimalFormat("###.00");
    return var0 < 1024L ? var0 + "bytes"
            : (var0 < 1048576L ? var2.format((double) ((float) var0 / 1024.0F)) + "KB"
                    : (var0 < 1073741824L ? var2.format((double) ((float) var0 / 1024.0F / 1024.0F)) + "MB"
                            : (var0 < 0L
                                    ? var2.format((double) ((float) var0 / 1024.0F / 1024.0F / 1024.0F)) + "GB"
                                    : "error")));
}