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 formatCurrency(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);/*from ww w .j av a 2 s.  c o  m*/
    return df.format(amount);
}

From source file:Main.java

public static String formatNumber(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);/*from w  w  w  . jav a 2 s  . c  om*/
    return df.format(amount);
}

From source file:com.aliyun.odps.ship.common.Util.java

public static String toReadableBytes(long bytes) {
    DecimalFormat df = new DecimalFormat("###,###.#");
    if (bytes < 1024) {
        return df.format(bytes) + " bytes";
    } else if (bytes < 1024 * 1024) {
        return df.format((float) bytes / 1024) + " KB";
    } else if (bytes < 1024 * 1024 * 1024) {
        return df.format((float) bytes / 1024 / 1024) + " MB";
    } else {/*w  w w.  j a  va 2  s .com*/
        return df.format((float) bytes / 1024 / 1024 / 1024) + " GB";
    }
}

From source file:com.alibaba.otter.manager.web.common.NumberFormatUtil.java

public static String format(Double data) {
    if (data == null) {
        return null;
    }//www  .j  a v a  2 s .com
    DecimalFormat format = new DecimalFormat(PATTERN);
    return format.format(data);
}

From source file:com.alibaba.otter.manager.web.common.NumberFormatUtil.java

public static String format(Integer data) {
    if (data == null) {
        return null;
    }/*  w  ww  .j  av  a  2  s  .  co  m*/
    DecimalFormat format = new DecimalFormat(PATTERN);
    return format.format(data);
}

From source file:com.alibaba.otter.manager.web.common.NumberFormatUtil.java

public static String format(Long data) {
    if (data == null) {
        return null;
    }//from   w  ww .j  a va2  s .  c o  m
    DecimalFormat format = new DecimalFormat(PATTERN);
    return format.format(data);
}

From source file:com.alibaba.otter.manager.web.common.NumberFormatUtil.java

public static String format(BigDecimal data) {
    if (data == null) {
        return null;
    }//w  w w  .j  a  v a  2 s  .c  o  m

    DecimalFormat format = new DecimalFormat(PATTERN);
    return format.format(data);
}

From source file:com.alibaba.otter.manager.web.common.NumberFormatUtil.java

public static String format(BigInteger data) {
    if (data == null) {
        return null;
    }/*www.  ja  v a 2  s.  co  m*/

    DecimalFormat format = new DecimalFormat(PATTERN);
    return format.format(data);
}

From source file:Main.java

private static String FormetFileSize(long fileS) {
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    String wrongSize = "0B";
    if (fileS == 0) {
        return wrongSize;
    }/*from   w  w w  . j a  va  2 s.c  om*/
    if (fileS < 1024) {
        fileSizeString = df.format((double) fileS) + "B";
    } else if (fileS < 1048576) {
        fileSizeString = df.format((double) fileS / 1024) + "KB";
    } else if (fileS < 1073741824) {
        fileSizeString = df.format((double) fileS / 1048576) + "MB";
    } else {
        fileSizeString = df.format((double) fileS / 1073741824) + "GB";
    }
    return fileSizeString;
}

From source file:net.nosleep.superanalyzer.util.Misc.java

public static String getFormattedPercentage(double percentage) {
    DecimalFormat wholeFormat = new DecimalFormat("0");
    return wholeFormat.format(percentage) + "%";
}