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.payu.sdk.helper.SignatureHelper.java

/**
 * The message the signature will use/*w  w w  .  ja  va2 s  .  c  o m*/
 *
 * @param order The order that will have the signature
 * @param merchantId The merchantId into the signature
 * @param key The apiKey of the merchant
 * @param valueFormat The format to use for decimal values
 * @return the message that will go into the signature
 */
private static String buildMessage(Order order, Integer merchantId, String key, String valueFormat) {

    validateOrder(order, merchantId);

    DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    df.applyPattern(valueFormat);

    StringBuilder message = new StringBuilder();
    message.append(key);
    message.append("~");
    message.append(merchantId);
    message.append("~");
    message.append(order.getReferenceCode());
    message.append("~");
    message.append(df.format(order.getAdditionalValue(TX_VALUE).getValue().doubleValue()));
    message.append("~");
    message.append(order.getAdditionalValue(TX_VALUE).getCurrency().toString());

    return message.toString();
}

From source file:net.ceos.project.poi.annotated.core.CellFormulaHandler.java

/**
 * Apply a BigDecimal value to the cell.
 * /*from w ww  . j  a  v  a 2 s  . c  om*/
 * @param configCriteria
 *            the {@link XConfigCriteria}
 * @param object
 *            the object
 * @param cell
 *            the {@link Cell} to use
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws ElementException
 */
protected static void bigDecimalHandler(final XConfigCriteria configCriteria, final Object object,
        final Cell cell)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ElementException {
    if (configCriteria.getElement().isFormula()) {
        // apply the formula
        if (!toFormula(configCriteria, cell)) {
            CellValueHandler.consumeValue(cell, toExplicitFormula(object, configCriteria.getField()));
        }
    } else {
        // normal manage cell
        BigDecimal bd = (BigDecimal) configCriteria.getField().get(object);

        if (bd != null) {
            Double dBigDecimal = bd.doubleValue();
            if (StringUtils.isNotBlank(configCriteria.getElement().transformMask())) {
                DecimalFormat df = new DecimalFormat(configCriteria.getElement().transformMask());
                CellValueHandler.consumeValue(cell,
                        df.format(dBigDecimal).replace(Constants.COMMA, Constants.DOT));
            } else {
                CellValueHandler.consumeValue(cell, dBigDecimal);
            }
        }
    }
}

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

/**
 * A number is converted to only have 2 places after the comma.
 * /*w w w .j ava  2  s  . com*/
 * @param value The number to convert to
 * @return Returns the number with only 2 places after the comma as String
 */
private static String resolveNumber2Places(String value) {
    Double number = Double.valueOf(value);
    DecimalFormat df = new DecimalFormat("0.00");
    return df.format(number);
}

From source file:com.reizes.shiva.utils.CommonUtil.java

/**
 * ?   (  ?)//from  ww w  .ja  v a2 s.c  o m
 * @param input
 * @return
 */
public static String decimalPointTwo(Float input) {
    if (input == null || input == 0) {
        return "0.0";
    }
    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.FLOOR);
    return df.format(input);
}

From source file:com.reizes.shiva.utils.CommonUtil.java

/**
 * ?   (  ?)/*  w  w  w . jav  a2 s  . c om*/
 * @param input
 * @return
 */
public static String decimalPointTwo(Double input) {
    if (input == null || input == 0) {
        return "0.0";
    }
    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.FLOOR);
    return df.format(input);
}

From source file:com.reizes.shiva.utils.CommonUtil.java

/**
 * ??? 100  ? ?? ? (  ?)/* ww  w  .java 2s. c  o  m*/
 *  ? null ? 0 ? 0.0 ( API   )
 * @param input  ?
 * @return ? ? ? ?
 */
public static String div100(Long input) {
    if (input == null || input == 0) {
        return "0.0";
    }
    Double fl = input / 100d;
    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.FLOOR);
    return df.format(fl);
}

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

/**
 * Adds a %-sign to a numeric value; in case the value is non-numeric, 
 * no modifications are performed./*from   w  ww .  j  a  va 2  s.c om*/
 */
private static String resolvePercentNoConvert(String value) {
    Double d = Double.valueOf(value); // throws exception if conversion fails

    DecimalFormat df = new DecimalFormat("0.00");
    return String.valueOf(df.format(d)) + "%";
}

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

/**
 * Displays a numeric value as percent (including conversion, i.e. multiplication with 100);
 * in case the value is non-numeric, no modifications are performed.
 *//*from www .j a  v  a 2  s. c o m*/
private static String resolvePercent(String value) {
    Double d = Double.valueOf(value); // throws exception if conversion fails
    double dPerc = d * 100;

    DecimalFormat df = new DecimalFormat("0.00");
    return String.valueOf(df.format(dPerc)) + "%";
}

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

/**
 * //from  w  w  w .  ja v a 2 s .  c  o  m
 * @param value
 * @return
 */
private static String resolveDecimalFromDouble(Double value) {

    DecimalFormat numberFormatter = new DecimalFormat("0");
    String out = numberFormatter.format(value);

    return out;
}

From source file:com.linkedin.drelephant.util.Utils.java

/**
 * Find percentage of numerator of denominator
 * @param numerator The numerator/*from   ww  w. j a v  a 2 s.  c o m*/
 * @param denominator The denominator
 * @return The percentage string of the form `x.yz %`
 */
public static String getPercentage(long numerator, long denominator) {

    if (denominator == 0) {
        return "NaN";
    }

    double percentage = ((double) numerator / (double) denominator) * 100;

    if ((long) (percentage) == 0) {
        return "0 %";
    }

    DecimalFormat df = new DecimalFormat("0.00");
    return df.format(percentage).concat(" %");
}