Example usage for java.text DecimalFormat applyPattern

List of usage examples for java.text DecimalFormat applyPattern

Introduction

In this page you can find the example usage for java.text DecimalFormat applyPattern.

Prototype

public void applyPattern(String pattern) 

Source Link

Document

Apply the given pattern to this Format object.

Usage

From source file:org.libreplan.web.common.Util.java

/**
 * Returns the value using the money format, that means, 2 figures for the
 * decimal part and concatenating the currency symbol from {@link Configuration} object.
 *//*from  w  ww.j  a  v a2s .  c  o  m*/
public static String addCurrencySymbol(BigDecimal value) {
    BigDecimal valueToReturn = value == null ? BigDecimal.ZERO : value;
    DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance();
    decimalFormat.applyPattern(getMoneyFormat());

    return decimalFormat.format(valueToReturn);
}

From source file:whitelabel.cloud.adapter.CurrencyFormatterExt.java

public NumberFormat getNumberFormat(Locale locale, String pattern) {

    NumberFormat df = NumberFormat.getNumberInstance(locale);
    DecimalFormat dec = (DecimalFormat) df;
    dec.applyPattern(pattern);
    dec.setMaximumFractionDigits(2);/*from   w  ww  .  j  a  va  2 s  .  c o m*/
    dec.setGroupingUsed(false);
    return dec;
}

From source file:capital.scalable.restdocs.jackson.BigDecimalSerializer.java

private DecimalFormat englishDecimalFormat() {
    DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH);
    df.applyPattern("#,##0.00");
    return df;//from   w w  w.ja  v a 2 s .  c  o m
}

From source file:com.safetys.framework.jmesa.core.filter.NumberFilterMatcher.java

public boolean evaluate(Object itemValue, String filterValue) {
    if (itemValue == null) {
        return false;
    }// w  ww . j av a 2  s.  co  m

    Locale locale = null;

    WebContext webContext = getWebContext();
    if (webContext != null) {
        locale = webContext.getLocale();
    }

    NumberFormat nf;
    if (locale != null) {
        nf = NumberFormat.getInstance(locale);
    } else {
        nf = NumberFormat.getInstance();
    }

    DecimalFormat df = (DecimalFormat) nf;
    String pattern = getPattern();
    df.applyPattern(pattern);
    itemValue = df.format(itemValue);

    String item = String.valueOf(itemValue);
    String filter = String.valueOf(filterValue);
    if (StringUtils.contains(item, filter)) {
        return true;
    }

    return false;
}

From source file:org.jmesa.core.filter.NumberFilterMatcher.java

@Override
public boolean evaluate(Object itemValue, String filterValue) {

    if (itemValue == null) {
        return false;
    }/*from w  w w .  java 2  s.c om*/

    Locale locale = null;

    WebContext webContext = getWebContext();
    if (webContext != null) {
        locale = webContext.getLocale();
    }

    NumberFormat nf;
    if (locale != null) {
        nf = NumberFormat.getInstance(locale);
    } else {
        nf = NumberFormat.getInstance();
    }

    DecimalFormat df = (DecimalFormat) nf;
    String pattern = getPattern();
    df.applyPattern(pattern);
    itemValue = df.format(itemValue);

    String item = String.valueOf(itemValue);
    String filter = String.valueOf(filterValue);
    if (StringUtils.contains(item, filter)) {
        return true;
    }

    return false;
}

From source file:org.talend.dataprofiler.chart.preview.DQRuleItemLabelGenerator.java

/**
 * DOC yyin Comment method "stringformat".
 * //from   w  w w .j ava2s  .  c  o  m
 * @param percent
 * @param i
 * @return
 */
private Object stringformat(Object percent, int i) {
    // ADD msjian TDQ-10793: when there is no data, the percent value is NaN
    if (Double.isNaN((double) percent)) {
        return String.valueOf(Double.NaN);
    }
    // TDQ-10793~

    BigDecimal zero = new BigDecimal(0);
    BigDecimal temp = new BigDecimal(percent.toString());
    BigDecimal min = new BigDecimal(10E-5);
    BigDecimal max = new BigDecimal(9999 * 10E-5);
    boolean isUseScientific = false;
    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
        isUseScientific = true;
    } else if (temp.compareTo(max) == 1 && temp.compareTo(new BigDecimal(1)) == -1) {
        percent = max.toString();
    }
    DecimalFormat format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH);
    format.applyPattern("0.00%"); //$NON-NLS-1$

    if (isUseScientific) {
        format.applyPattern("0.###E0%"); //$NON-NLS-1$
    }
    return format.format(new Double(percent.toString()));
}

From source file:gda.gui.beans.PVScannableBean.java

@Override
protected void refreshValues() {
    synchronized (this) {
        try {/*from  w  ww  .  j  ava2s .c o m*/
            // get the units
            Object unit = theScannable.getAttribute(PVScannable.UNITSATTRIBUTE);
            if (unit != null) {
                unitsString = unit.toString();
            } else {
                unitsString = "";
            }

            // generate the tooltip
            tooltipString = theScannable.getName() + " ";

            // display the current value
            Double currentPosition = ScannableUtils.getCurrentPositionArray(theScannable)[0];
            if (isZeroSmallNumbers() && currentPosition < 0.0001) {
                DecimalFormat myFormat = new DecimalFormat();
                myFormat.applyPattern("#####.###");
                valueString = myFormat.format(currentPosition).trim();
            } else {
                valueString = String.format("%5.3g", currentPosition).trim();
            }
        } catch (DeviceException e) {
            logger.error("Exception while trying to move " + scannableName + ": " + e.getMessage());
        }
    }
}

From source file:DecimalFormatDemo.java

/** Reformats the input number and displays result. */
public void reformat() {
    try {/*from   www . java2 s.  co m*/
        NumberFormat nf = NumberFormat.getNumberInstance(availableLocales.getCurrent());
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern(currentPattern);
        result.setForeground(Color.black);
        result.setText(df.format(currentNumber));
    } catch (IllegalArgumentException iae) {
        result.setForeground(Color.red);
        result.setText("Illegal Pattern: " + iae.getMessage());
    }
}

From source file:gda.gui.beans.ScannableMotionUnitsBean.java

@Override
protected synchronized void refreshValues() {
    try {/*from   www. j  av a 2s  .  c  om*/
        // get the units
        unitsString = theScannable.getAttribute(ScannableMotionUnits.USERUNITS).toString();

        // generate the tooltip
        String toolTip = theScannable.getName();
        {
            Double[] limits = (Double[]) theScannable.getAttribute(ScannableMotion.FIRSTINPUTLIMITS);
            if (limits != null) {
                toolTip += " (" + ((limits[0] != null) ? limits[0] : "") + ", "
                        + ((limits[1] != null) ? limits[1] : "") + ")";
            }
        }
        tooltipString = toolTip;

        // display the current value
        Double currentPosition = ScannableUtils.getCurrentPositionArray(theScannable)[0];
        if (isZeroSmallNumbers() && (math.fabs(currentPosition) < 0.0001)) {
            DecimalFormat myFormat = new DecimalFormat();
            myFormat.applyPattern("#####.####");
            String newText = myFormat.format(currentPosition);
            valueString = newText.trim();
        } else {
            String newText = String.format(getDisplayFormat(), currentPosition);
            valueString = newText.trim();
        }
    } catch (DeviceException e) {
        logger.error("Exception while trying to update display " + scannableName + ": " + e.getMessage());
    }
}

From source file:org.sejda.core.support.prefix.processor.NumberPrefixProcessor.java

/**
 * @param numberPattern/* w  w  w  . j a v a2 s. c om*/
 *            the input number pattern of the type "####"
 * @return the {@link DecimalFormat} with the applied pattern
 */
private DecimalFormat formatter(String numberPattern) {
    DecimalFormat retVal = new DecimalFormat();
    try {
        if (StringUtils.isNotBlank(numberPattern)) {
            retVal.applyPattern(numberPattern.replaceAll("#", "0"));
            return retVal;
        }
    } catch (IllegalArgumentException iae) {
        LOG.error(String.format("Error applying pattern %s", numberPattern), iae);
    }
    retVal.applyPattern("00000");
    return retVal;
}