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 doubleToString(double num) {
    DecimalFormat df = new DecimalFormat("0.00");
    return df.format(num);
}

From source file:Main.java

public static DecimalFormat createDecimalFormat(String pattern) {
    return new DecimalFormat(pattern);
}

From source file:Main.java

public static String handleDouble(double price) {
    DecimalFormat decimalFormat = new DecimalFormat("##.##");
    return decimalFormat.format(price);
}

From source file:Main.java

public static String formatFileSizeToString(long fileLen) {
    DecimalFormat df = new DecimalFormat("0.00");
    String fileSizeString = "";
    if (fileLen < 1024) {
        fileSizeString = df.format((double) fileLen) + "B";
    } else if (fileLen < 1048576) {
        fileSizeString = df.format((double) fileLen / 1024) + "K";
    } else if (fileLen < 1073741824) {
        fileSizeString = df.format((double) fileLen / 1048576) + "M";
    } else {//from  w  w  w.j av a 2s  . co m
        fileSizeString = df.format((double) fileLen / 1073741824) + "G";
    }
    return fileSizeString;
}

From source file:Main.java

public static String getRoundedDistanceString(double distance) {
    return new DecimalFormat("##0.00").format(getRoundedDistance(distance));
}

From source file:Main.java

public static String decimalFormat(String s, String format) {
    DecimalFormat decimalFormat = new DecimalFormat(format);
    return decimalFormat.format(s);
}

From source file:Main.java

/**
 * Convert filesize unit// w  w  w  .  j  av a 2  s  .  c om
 * 
 * @param size
 * @return
 */
public static String formetFileSize(long size) {
    DecimalFormat df = new DecimalFormat("#0.00");
    String fileSizeString = "";
    if (size < 1024) {
        fileSizeString = df.format((double) size) + "B";
    } else if (size < 1048576) {
        fileSizeString = df.format((double) size / 1024) + "K";
    } else if (size < 1073741824) {
        fileSizeString = df.format((double) size / 1048576) + "M";
    } else {
        fileSizeString = df.format((double) size / 1073741824) + "G";
    }
    return fileSizeString;
}

From source file:net.kamhon.ieagle.util.TimeUtil.java

public static String toTime(int hours, int minutes, int seconds) {
    DecimalFormat df = new DecimalFormat("00");

    return df.format(hours) + ":" + df.format(minutes) + ":" + df.format(seconds);
}

From source file:Main.java

/**
 * Get the file size in a human-readable string.
 * /*  w  ww  . j ava 2s  . c  o m*/
 * @param size
 * @return
 * 
 * @author paulburke
 */
public static String getReadableFileSize(int size) {
    final int BYTES_IN_KILOBYTES = 1024;
    final DecimalFormat dec = new DecimalFormat("###.#");
    final String KILOBYTES = " KB";
    final String MEGABYTES = " MB";
    final String GIGABYTES = " GB";
    float fileSize = 0;
    String suffix = KILOBYTES;

    if (size > BYTES_IN_KILOBYTES) {
        fileSize = size / BYTES_IN_KILOBYTES;
        if (fileSize > BYTES_IN_KILOBYTES) {
            fileSize = fileSize / BYTES_IN_KILOBYTES;
            if (fileSize > BYTES_IN_KILOBYTES) {
                fileSize = fileSize / BYTES_IN_KILOBYTES;
                suffix = GIGABYTES;
            } else {
                suffix = MEGABYTES;
            }
        }
    }
    return String.valueOf(dec.format(fileSize) + suffix);
}

From source file:Main.java

/**
 * byte convert//from  www . j a  v  a2 s  .co  m
 * @param size like 3232332
 * @return like 3.23M
 */
//   public static String getFormatSize(long size){
//      if (size >= 1024 * 1024 * 1024){
//         Double dsize = (double) (size / (1024 * 1024 * 1024));
//         return new DecimalFormat("#.00").format(dsize) + "G";
//      }else if (size >= 1024 * 1024) {
//         Double dsize = (double) (size / (1024 * 1024));
//         return new DecimalFormat("#.00").format(dsize) + "M";
//      }else if (size >= 1024) {
//         Double dsize = (double) (size / 1024);
//         return new DecimalFormat("#.00").format(dsize) + "K";
//      }else {
//         return String.valueOf((int)size) + "B";
//      }
//   }

public static String getFormatSize(double size) {
    if (size >= 1024 * 1024 * 1024) {
        Double dsize = size / (1024 * 1024 * 1024);
        return new DecimalFormat("#.00").format(dsize) + "G";
    } else if (size >= 1024 * 1024) {
        Double dsize = size / (1024 * 1024);
        return new DecimalFormat("#.00").format(dsize) + "M";
    } else if (size >= 1024) {
        Double dsize = size / 1024;
        return new DecimalFormat("#.00").format(dsize) + "K";
    } else {
        return String.valueOf((int) size) + "B";
    }
}