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:com.linkedin.drelephant.util.Utils.java

/**
 * Convert a value in MBSeconds to GBHours
 * @param MBSeconds The value to convert
 * @return A string of form a.xyz GB Hours
 *//*from   w w  w . j a va 2 s  . c  o  m*/
public static String getResourceInGBHours(long MBSeconds) {

    if (MBSeconds == 0) {
        return "0 GB Hours";
    }

    double GBHours = MBSecondsToGBHours(MBSeconds);
    if ((long) (GBHours * 1000) == 0) {
        return "0 GB Hours";
    }

    DecimalFormat df = new DecimalFormat("0.000");
    String GBHoursString = df.format(GBHours);
    GBHoursString = GBHoursString + " GB Hours";
    return GBHoursString;
}

From source file:jsdp.app.lotsizing.sS_jsdp.java

public static void solveSampleInstanceForwardRecursion(Distribution[] distributions, double minDemand,
        double maxDemand, double fixedOrderingCost, double proportionalOrderingCost, double holdingCost,
        double penaltyCost, double initialInventory, double confidence, double errorTolerance) {

    sS_ForwardRecursion recursion = new sS_ForwardRecursion(distributions, minDemand, maxDemand,
            fixedOrderingCost, proportionalOrderingCost, holdingCost, penaltyCost);

    sS_StateDescriptor initialState = new sS_StateDescriptor(0, sS_State.inventoryToState(initialInventory));

    /**//from  w  w  w .jav a2s  .c  o  m
     * Replace parallelStream with stream in "runForwardRecursion" to prevent deadlock
     */
    StopWatch timer = new StopWatch();
    timer.start();
    recursion.runForwardRecursion(
            ((sS_StateSpace) recursion.getStateSpace()[initialState.getPeriod()]).getState(initialState));
    timer.stop();
    double ETC = recursion.getExpectedCost(initialInventory);
    double action = sS_State.stateToInventory(recursion.getOptimalAction(initialState).getIntAction());
    System.out.println(
            "Expected total cost (assuming an initial inventory level " + initialInventory + "): " + ETC);
    System.out.println("Optimal initial action: " + action);
    System.out.println("Time elapsed: " + timer);
    System.out.println();

    double[][] optimalPolicy = recursion.getOptimalPolicy(initialInventory);
    double[] s = optimalPolicy[0];
    double[] S = optimalPolicy[1];
    for (int i = 0; i < distributions.length; i++) {
        System.out.println("S[" + (i + 1) + "]:" + S[i] + "\ts[" + (i + 1) + "]:" + s[i]);
    }
    System.out.println("Note that (s,S) values for period 0 have been set on best effort basis.");

    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    DecimalFormat df = new DecimalFormat("#.00", otherSymbols);

    double[] results = SimulatePolicies.simulate_sS(distributions, fixedOrderingCost, holdingCost, penaltyCost,
            proportionalOrderingCost, initialInventory, recursion, confidence, errorTolerance);
    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:jsdp.app.inventory.univariate.CapacitatedStochasticLotSizing.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);

    double[] results = SimulatePolicies.simulateStochaticLotSizing(distributions, fixedOrderingCost,
            holdingCost, penaltyCost, proportionalOrderingCost, initialInventory, recursion, confidence,
            errorTolerance);// www  . j a v  a 2 s . c  om
    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:com.nubits.nubot.utils.Utils.java

public static String formatNumber(double number, int digits) {
    DecimalFormat df = new DecimalFormat("0");
    df.setMaximumFractionDigits(digits);
    return df.format(number);
}

From source file:com.feilong.core.text.NumberFormatUtil.java

/**
 *  {@link Number}  {@link RoundingMode} <code>numberPattern</code>?.
 *
 * @param value//from w  ww .  j  av a  2 s. c  o  m
 *            the value
 * @param numberPattern
 *            the pattern {@link NumberPattern}
 * @param roundingMode
 *            ?{@link RoundingMode}
 * @return  <code>value</code> null, {@link NullPointerException}<br>
 *          <code>numberPattern</code> null, {@link NullPointerException}<br>
 *          <code>numberPattern</code> blank, {@link IllegalArgumentException}<br>
 * @see DecimalFormat
 * @see <a href="../util/NumberUtil.html#RoundingMode">JAVA 8??</a>
 */
public static String format(Number value, String numberPattern, RoundingMode roundingMode) {
    Validate.notNull(value, "value can't be null!");
    Validate.notBlank(numberPattern, "numberPattern can't be null!");

    // applyPattern(pattern, false)
    DecimalFormat decimalFormat = new DecimalFormat(numberPattern);

    // ? RoundingMode.HALF_EVEN  ?,?.?,?.??,?,??.???1?,???.
    // 1.15>1.2 1.25>1.2
    if (null != roundingMode) {
        decimalFormat.setRoundingMode(roundingMode);
    }
    String result = decimalFormat.format(value);
    LOGGER.trace("input:[{}],with:[{}]=[{}],localizedPattern:[{}]", value, numberPattern, result,
            decimalFormat.toLocalizedPattern());
    return result;
}

From source file:com.fluidops.iwb.api.ValueResolver.java

/**
 * A String is converted into a long and than calculated to the set unit (MB, GB, TB).
 * //from   www .ja  va 2s .  com
 * @param value The String to convert to long
 * @param unit The Unit in which the value is converted
 * @return Returns String representation of the changed long value with unit
 */
private static String resolveKByte2X(String value, String unit) {
    Double kByteSize = Double.valueOf(value);
    DecimalFormat df = new DecimalFormat("0.00");

    if (unit.equals("MB"))
        return String.valueOf(df.format(kByteSize / 1024.0)) + " " + unit;
    else if (unit.equals("GB"))
        return String.valueOf(df.format(kByteSize / 1048576.0)) + " " + unit;
    else if (unit.equals("TB"))
        return String.valueOf(df.format(kByteSize / 1073741824.0)) + " " + unit;
    else
        return "<em>" + StringEscapeUtils.escapeHtml(value) + " could not converted to "
                + StringEscapeUtils.escapeHtml(unit) + "!</em>";
}

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

/***
 * //from   ww w . ja v a 2s .co  m
 * @param stringToFormat
 * @param numberOfDecimals can be any number, also negative as the used DecimalFormatter accepts it and makes it 0
 * @return
 */
public static String formatNumberWithDecimals(String stringToFormat, int minimalNumberOfDecimals,
        int maximalNumberOfDecimals) {
    if (stringToFormat == null || stringToFormat.length() == 0) {
        return null;
    }

    String result = null;

    DecimalFormat formatter = new DecimalFormat();
    setupFormatter(formatter, minimalNumberOfDecimals, maximalNumberOfDecimals);

    result = formatter.format(Double.parseDouble(stringToFormat));

    return result;
}

From source file:com.fluidops.iwb.api.ValueResolver.java

/**
 * A String is converted into a long and than calculated to the set unit (KB, MB, GB, TB).
 * /*from www . ja  va  2s . c  o  m*/
 * @param value The String to convert to long
 * @param unit The Unit in which the value is converted
 * @return Returns String representation of the changed long value with unit
 */
private static String resolveByte2X(String value, String unit) {
    Double byteSize = Double.valueOf(value);
    DecimalFormat df = new DecimalFormat("0.00");

    if (unit.equals("KB"))
        return String.valueOf(df.format(byteSize / 1024.0)) + " " + unit;
    else if (unit.equals("MB"))
        return String.valueOf(df.format(byteSize / 1048576.0)) + " " + unit;
    else if (unit.equals("GB"))
        return String.valueOf(df.format(byteSize / 1073741824.0)) + " " + unit;
    else if (unit.equals("TB"))
        return String.valueOf(df.format((byteSize / 1073741824.0) / 1024.0)) + " " + unit;
    else
        return "<em>" + StringEscapeUtils.escapeHtml(value) + " could not be converted to "
                + StringEscapeUtils.escapeHtml(unit) + "!</em>";
}

From source file:jsdp.app.lotsizing.sS_jsdp.java

public static void solveSampleInstanceBackwardRecursion(Distribution[] distributions, double minDemand,
        double maxDemand, double fixedOrderingCost, double proportionalOrderingCost, double holdingCost,
        double penaltyCost, double initialInventory, double confidence, double errorTolerance,
        sS_StateSpaceSampleIterator.SamplingScheme samplingScheme, int maxSampleSize) {

    sS_BackwardRecursion recursion = new sS_BackwardRecursion(distributions, minDemand, maxDemand,
            fixedOrderingCost, proportionalOrderingCost, holdingCost, penaltyCost, samplingScheme,
            maxSampleSize);//from  w  w  w. ja va2s  .  c o m
    /*sS_SequentialBackwardRecursion recursion = new sS_SequentialBackwardRecursion(distributions,
                                                                            fixedOrderingCost,
                                                                            proportionalOrderingCost,
                                                                            holdingCost,
                                                                            penaltyCost);*/

    StopWatch timer = new StopWatch();
    timer.start();
    recursion.runBackwardRecursion();
    timer.stop();
    System.out.println();
    double ETC = recursion.getExpectedCost(initialInventory);
    System.out.println(
            "Expected total cost (assuming an initial inventory level " + initialInventory + "): " + ETC);
    System.out.println("Time elapsed: " + timer);
    System.out.println();

    double[][] optimalPolicy = recursion.getOptimalPolicy(initialInventory);
    double[] s = optimalPolicy[0];
    double[] S = optimalPolicy[1];
    for (int i = 0; i < distributions.length; i++) {
        System.out.println("S[" + (i + 1) + "]:" + S[i] + "\ts[" + (i + 1) + "]:" + s[i]);
    }

    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    DecimalFormat df = new DecimalFormat("#.00", otherSymbols);

    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:edu.isi.misd.scanner.network.modules.worker.processors.oceans.OceansLogisticRegressionProcessor.java

/**
 * Formats a double to a fixed (currently three) number of decimal places.
 *//*  w w  w . j  a va  2s .  c  om*/
public static double df(double a) {
    DecimalFormat f = new DecimalFormat(".000");
    Double input = Double.valueOf(a);
    // check for special values so that they are not parsed
    if (input.equals(Double.NEGATIVE_INFINITY) || input.equals(Double.POSITIVE_INFINITY)
            || input.equals(Double.NaN)) {
        return input.doubleValue();
    }
    return Double.parseDouble(f.format(input));
}