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 double calculateMinWeight(double height) {
    DecimalFormat format = new DecimalFormat("0.0");
    double result = 18.5 * height * height / 10000;
    return Double.valueOf(format.format(result).toString());
}

From source file:Main.java

public static double calculateMaxWeight(double height) {
    DecimalFormat format = new DecimalFormat("0.0");
    double result = 24.0 * height * height / 10000;
    return Double.valueOf(format.format(result).toString());
}

From source file:Main.java

public static double calculateBMI(double weight, double height) {
    DecimalFormat format = new DecimalFormat("0.00");
    double result = weight / (height * height) * 10000;
    return Double.valueOf(format.format(result).toString());
}

From source file:Main.java

public static double asMiles(final long meters) {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(asMiles(meters / 1000.0)));
}

From source file:Main.java

public static String formatFileSize(long fileS) {
    String fileSizeString = "";
    if (fileS <= 0) {
        return "0 KB";
    }/*w  ww. jav a2s .  c  o  m*/
    if (fileS < 1024) {
        fileSizeString = "1 KB";
    } else if (fileS < 1048576) {
        long inte = fileS / 1024;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.00");
            fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
        }
    } else if (fileS < 1073741824) {
        long inte = fileS / 1048576;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.00");
            fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
        }
    } else if (fileS < 1099511627776L) {
        long inte = fileS / 1073741824;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
        }
    } else {
        long inte = fileS / 1099511627776L;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.00");
            fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
        }
    }
    return fileSizeString;
}

From source file:Main.java

public static String formatMoney(String amount) {
    if (TextUtils.isEmpty(amount)) {
        return "0.00";
    }//from   w w  w . ja  v a  2  s . 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;
}

From source file:Main.java

public static double calculateBMR(int sex, double weight, double height, int age) {
    double result = 0.0;
    DecimalFormat format = new DecimalFormat("0.00");
    if (sex == UserSexMale) {
        result = 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
    } else {/*from  www.j av  a2 s . c o  m*/
        result = 66 + (13.8 * weight) + (5.0 * height) - (6.8 * age);
    }
    return Double.valueOf(format.format(result).toString());
}

From source file:Main.java

public static String getReadableSize(long bytes) {
    if (bytes <= 0)
        return "0";
    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
    return new DecimalFormat("#,##0.#").format(bytes / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

From source file:Main.java

static String formatTextSize(float textPxSize, String unit) {
    return new DecimalFormat("0").format(textPxSize) + unit;
}

From source file:Main.java

/**
 * //w  w  w . jav a2s . co m
 * @Title: formatCacheMeasure: format files' measure
 * @Description: formatCacheMeasure
 * @param @param path
 * @param @return
 * @return String
 * @throws @param path
 * @throws @return
 */
public static String formatCacheMeasure(String path) {
    String measure;
    DecimalFormat df = new DecimalFormat(CACHE_MEASURE_FORMATE);
    getCacheMeasure(path);
    if (mCacheMeasure == 0.0) {
        mCacheMeasure = 0;
        return null;
    } else if (mCacheMeasure < CACHESIZE1) {
        measure = df.format((double) mCacheMeasure / CACHESIZE0) + "K";
    } else if (mCacheMeasure < 1073741824) {
        measure = df.format((double) mCacheMeasure / CACHESIZE1) + "M";
    } else {
        measure = df.format((double) mCacheMeasure / CACHESIZE2) + "G";
    }
    mCacheMeasure = 0;
    return measure;
}