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:net.naijatek.myalumni.util.utilities.FileUtil.java

public static String getHumanSize(final long size) {

    int sizeToStringLength = String.valueOf(size).length();
    String humanSize = "";
    DecimalFormat formatter = new DecimalFormat("##0.##");
    if (sizeToStringLength > 9) {
        humanSize += formatter.format((double) size / (1024 * 1024 * 1024)) + " GB";
    } else if (sizeToStringLength > 6) {
        humanSize += formatter.format((double) size / (1024 * 1024)) + " MB";
    } else if (sizeToStringLength > 3) {
        humanSize += formatter.format((double) size / 1024) + " KB";
    } else {/*from ww  w. j  a  v a2  s.  c  o  m*/
        humanSize += String.valueOf(size) + " Bytes";
    }
    return humanSize;
}

From source file:com.mendhak.gpslogger.common.OpenGTSClient.java

/**
 * Encode a location as GPRMC string data.
 * <p/>/* w w w  . j  a  v  a 2  s .  c  o m*/
 * For details check org.opengts.util.Nmea0183#_parse_GPRMC(String)
 * (OpenGTS source)
 *
 * @param loc location
 * @return GPRMC data
 */
public static String GPRMCEncode(SerializableLocation loc) {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    DecimalFormat f = new DecimalFormat("0.000000", dfs);

    String gprmc = String.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,,", "$GPRMC",
            NMEAGPRMCTime(new Date(loc.getTime())), "A", NMEAGPRMCCoord(Math.abs(loc.getLatitude())),
            (loc.getLatitude() >= 0) ? "N" : "S", NMEAGPRMCCoord(Math.abs(loc.getLongitude())),
            (loc.getLongitude() >= 0) ? "E" : "W", f.format(MetersPerSecondToKnots(loc.getSpeed())),
            f.format(loc.getBearing()), NMEAGPRMCDate(new Date(loc.getTime())));

    gprmc += "*" + NMEACheckSum(gprmc);

    return gprmc;
}

From source file:com.iCo6.iConomy.java

/**
 * Formats the money in a human readable form with the currency attached:<br /><br />
 * 20000.53 = 20,000.53 Coin<br />
 * 20000.00 = 20,000 Coin//from   w w w .j  a  va 2 s .  co m
 *
 * @param amount double
 * @return String
 */
public static String format(double amount) {
    DecimalFormat formatter = new DecimalFormat("#,##0.00");
    String formatted = formatter.format(amount);

    if (formatted.endsWith(".")) {
        formatted = formatted.substring(0, formatted.length() - 1);
    }

    return Common.formatted(formatted, Constants.Nodes.Major.getStringList(),
            Constants.Nodes.Minor.getStringList());
}

From source file:jsdp.app.inventory.univariate.StochasticLotSizing.java

static void simulate(Distribution[] distributions, double fixedOrderingCost, double holdingCost,
        double penaltyCost, double proportionalOrderingCost, double initialInventory,
        BackwardRecursionImpl recursion, double confidence, double errorTolerance) {
    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    DecimalFormat df = new DecimalFormat("#.00", otherSymbols);

    sS_Policy policy = new sS_Policy(recursion, distributions.length);
    double[][] optimalPolicy = policy.getOptimalPolicy(initialInventory);
    double[] s = optimalPolicy[0];
    double[] S = optimalPolicy[1];
    for (int i = 0; i < distributions.length; i++) {
        System.out.println("S[" + (i + 1) + "]:" + df.format(S[i]) + "\ts[" + (i + 1) + "]:" + df.format(s[i]));
    }/*from w w w. j  av a2 s .  co  m*/

    double[] results = SimulatePolicies.simulate_sS(distributions, fixedOrderingCost, holdingCost, penaltyCost,
            proportionalOrderingCost, initialInventory, S, s, confidence, errorTolerance);
    System.out.println();
    System.out.println("Simulated cost: " + df.format(results[0]) + " Confidence interval=("
            + df.format(results[0] - results[1]) + "," + df.format(results[0] + results[1]) + ")@"
            + df.format(confidence * 100) + "% confidence");
    System.out.println();
}

From source file:PodbaseMetadataMigration2.java

private static String formatMs(double ms) {
    DecimalFormat df = new DecimalFormat("###0.00");
    if (ms < 3000)
        return ms + "ms";
    if (ms < 1000 * 60 * 3)
        return df.format((double) ms / 1000) + "s";
    int minutes = (int) (ms / (1000 * 60));
    int seconds = (int) ((ms - minutes * 1000 * 60) / 1000);
    return String.format("%d minutes, %d seconds", minutes, seconds);
}

From source file:com.pureinfo.srm.reports.table.MyTabeleDataHelper.java

public static String getRatioString(double n, double d) {
    if (d < 0.0001) {
        return "----";
    }//from  w  ww .  ja v  a  2 s  . c  o  m
    DecimalFormat format = new DecimalFormat("#0.0%");
    return format.format(n / d);
}

From source file:com.kingcore.framework.util.ConvertUtils.java

/**
 * ??/*  www.j a v  a 2s .  c  o m*/
 * @param double ?
 * @return String
 */
public static String makeUpperCaseSum(double je) {

    if (je < 0)
        je *= -1;

    final String[] upper_number = { "", "", "??", "", "?", "", "", "?", "", "" };
    final String[] number_dw = { "", "", "", "", "", "", "", "", "", "", "",
            "", "", "" };
    String operate, upper_str, vsDx;
    int i, j, point_pos, int_len;
    if (je < 0.01)
        return "";

    upper_str = "";

    DecimalFormat aFormat = (DecimalFormat) new java.text.DecimalFormat("##0.00");
    operate = new String(aFormat.format(je));

    point_pos = operate.indexOf(".");
    if (point_pos == -1)
        int_len = operate.length();
    else {
        if (operate.length() - point_pos <= 2)
            operate += "0";
        int_len = point_pos;
    }
    if (int_len > number_dw.length - 1)
        return "too long ???";

    if (je > 0) {
        for (i = 0; i < int_len; i++) {
            j = int_len - i;
            if (i > 0 && "0".equals(operate.substring(i, i + 1))) {
                String sss = number_dw[j];
                if (!"0".equals(operate.substring(i - 1, i)) && !"".equals(sss) && !"".equals(sss)
                        && !"".equals(sss))
                    upper_str = upper_str + "";
                else if ("".equals(number_dw[j]) || "".equals(number_dw[j])
                        || "".equals(number_dw[j])) {
                    int ssss = upper_str.length() - 1;
                    String sssss = upper_str.substring(ssss);
                    if (!sssss.equals("")) {
                        if (!upper_str.substring(upper_str.length() - 1).equals("")
                                && !upper_str.substring(upper_str.length() - 1).equals("")) {
                            if (upper_str.substring(upper_str.length() - 1).equals("")
                                    && !number_dw[j].equals(""))
                                upper_str = upper_str + number_dw[j] + "";
                            else
                                upper_str = upper_str + number_dw[j];
                        }
                    } else {
                        if (!upper_str.substring(upper_str.length() - 2, upper_str.length() - 1).equals("")
                                && !upper_str.substring(upper_str.length() - 2, upper_str.length() - 1)
                                        .equals("")
                                || number_dw[j].equals("")) {
                            upper_str = upper_str.substring(0, upper_str.length() - 1) + number_dw[j];
                            if (!number_dw[j].equals("")) {
                                upper_str = upper_str + "";
                            }
                        }
                    }
                }
            } else {
                if (!operate.substring(i, i + 1).equals("0")) {
                    int k = Integer.parseInt(operate.substring(i, i + 1));
                    upper_str = upper_str + upper_number[k - 1] + number_dw[j];
                }
            }
        }
    }
    if (point_pos > 0) {
        if (!operate.substring(point_pos + 1, point_pos + 2).equals("0")) {
            int k = Integer.parseInt(operate.substring(point_pos + 1, point_pos + 2));
            upper_str = upper_str + upper_number[k - 1] + "";
            if (!operate.substring(point_pos + 2, point_pos + 3).equals("0")) {
                int m = Integer.parseInt(operate.substring(point_pos + 2, point_pos + 3));
                upper_str = upper_str + upper_number[m - 1] + "";
            }
        } else {
            if (!operate.substring(point_pos + 2, point_pos + 3).equals("0")) {
                int k = Integer.parseInt(operate.substring(point_pos + 2, point_pos + 3));
                upper_str = upper_str + "" + upper_number[k - 1] + "";
            }
        }
    }

    if (!upper_str.substring(upper_str.length() - 1).equals("") && !upper_str.equals(""))
        upper_str = upper_str + "";

    return upper_str;
}

From source file:de.micromata.genome.gwiki.controls.GWikiPageInfoActionBean.java

public static String formatAttachmentSize(String size) {
    if (NumberUtils.isDigits(size) == false) {
        return size;
    }//from ww w .  j  av  a2s . com
    long numb = NumberUtils.toLong(size);
    DecimalFormat nf = new DecimalFormat();
    String ret = nf.format(numb);
    return ret;
}

From source file:com.weibo.api.motan.util.StatsUtil.java

public static String memoryStatistic() {
    Runtime runtime = Runtime.getRuntime();

    double freeMemory = (double) runtime.freeMemory() / (1024 * 1024);
    double maxMemory = (double) runtime.maxMemory() / (1024 * 1024);
    double totalMemory = (double) runtime.totalMemory() / (1024 * 1024);
    double usedMemory = totalMemory - freeMemory;
    double percentFree = ((maxMemory - usedMemory) / maxMemory) * 100.0;

    double percentUsed = 100 - percentFree;

    DecimalFormat mbFormat = new DecimalFormat("#0.00");
    DecimalFormat percentFormat = new DecimalFormat("#0.0");

    StringBuilder sb = new StringBuilder();
    sb.append(mbFormat.format(usedMemory)).append("MB of ").append(mbFormat.format(maxMemory)).append(" MB (")
            .append(percentFormat.format(percentUsed)).append("%) used");
    return sb.toString();
}

From source file:com.itude.mobile.mobbl.core.util.StringUtilities.java

public static String formatNumberWithOriginalNumberOfDecimals(String stringToFormat) {

    if (stringToFormat == null || stringToFormat.length() == 0) {
        return null;
    }/*from w  w w. j  a va  2  s  .  c  om*/

    String result = null;

    DecimalFormat formatter = new DecimalFormat("#################.####################",
            new DecimalFormatSymbols(getDefaultFormattingLocale()));

    try {
        result = formatter.format(Double.parseDouble(stringToFormat));
    } catch (Exception e) {
        LOG.warn("Could not format string " + stringToFormat
                + " as number with original number of decimals (StringUtilities)", e);

        return null;
    }

    return result;
}