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:de.awtools.basic.AWTools.java

/**
 * Wandelt einen double Wert in einen String anhand des angegebenen Patterns.
 * Siehe zu diesem Thema auch im Java-Tutorial bzw. API.
 *
 * <p>/*from ww  w  .j a  v  a 2 s  .c  o  m*/
 * Apply the given pattern to this Format object. A pattern is a short-hand
 * specification for the various formatting properties. These properties can
 * also be changed individually through the various setter methods. There
 * is no limit to integer digits set by this routine, since that is the 
 * typical end-user desire; use setMaximumInteger if you want to set a 
 * real value. For negative numbers, use a second pattern, separated by a semicolon
 * </p>
 * <p>
 * Example "#,#00.0#" -> 1,234.56
 * This means a minimum of 2 integer digits, 1 fraction digit, and a maximum
 * of 2 fraction digits.
 * </p>
 * <p>
 * Example: "#,#00.0#;(#,#00.0#)" for negatives in parentheses.
 * In negative patterns, the minimum and maximum counts are ignored; these
 * are presumed to be set in the positive pattern.
 * </p>
 * 
 * @param value Der zu konvertierende Wert.
 * @param pattern Das zu verwendende Pattern.
 * @param locale Das zu verwendende Locale.
 * @return Formatiert einen <code>double</code>.
 */
public static String toString(final double value, String pattern, final Locale locale) {

    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.applyPattern(pattern);
    return df.format(value);
}

From source file:org.toobsframework.transformpipeline.xslExtentions.PriceFormatHelper.java

/**
 * Gets a string that represents the input Price formatted into the proper fromate, and converted
 * into the proper timezone./*from w  w w .  j  a v a 2  s .c  om*/
 *
 * @return Price-only string formatted with given time zone.
 *
 * @exception XMLTransfromerException if parsing problem occurs
 */
public static String getFormattedPrice(String inputPrice, String priceFormat, String language)
        throws XMLTransformerException {
    if ((inputPrice == null) || (inputPrice.trim().length() == 0)) {
        inputPrice = "0";
    }

    Locale locale = new Locale(language.substring(2, 4).toLowerCase(), language.substring(0, 2));
    DecimalFormat priceFormatter = (DecimalFormat) NumberFormat.getNumberInstance(locale);
    priceFormatter.setGroupingUsed(true);
    priceFormatter.setMaximumFractionDigits(2);
    priceFormatter.setMinimumFractionDigits(2);

    priceFormatter.applyPattern(priceFormat);

    return priceFormatter.format(new Double(inputPrice));
}

From source file:org.dspace.app.util.Util.java

/**
  * Formats the file size. Examples://  w ww.  ja v  a2 s . c om
  *
  *  - 50 = 50B
  *  - 1024 = 1KB
  *  - 1,024,000 = 1MB etc
  *
  *  The numbers are formatted using java Locales
  *
  * @param in The number to convert
  * @return the file size as a String
  */
public static String formatFileSize(double in) {
    // Work out the size of the file, and format appropriatly
    // FIXME: When full i18n support is available, use the user's Locale
    // rather than the default Locale.
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
    DecimalFormat df = (DecimalFormat) nf;
    df.applyPattern("###,###.##");
    if (in < 1024) {
        df.applyPattern("0");
        return df.format(in) + " " + "B";
    } else if (in < 1024000) {
        in = in / 1024;
        return df.format(in) + " " + "kB";
    } else if (in < 1024000000) {
        in = in / 1024000;
        return df.format(in) + " " + "MB";
    } else {
        in = in / 1024000000;
        return df.format(in) + " " + "GB";
    }
}

From source file:Main.java

public static String IndianFormat(BigDecimal n) {
    DecimalFormat formatter = new DecimalFormat("#,###.00");
    //we never reach double digit grouping so return
    if (n.doubleValue() < 100000) {
        return formatter.format(n.setScale(2, 1).doubleValue());
    }/*www  . j a v  a  2 s  . c  om*/
    StringBuffer returnValue = new StringBuffer();
    //Spliting integer part and decimal part
    String value = n.setScale(2, 1).toString();
    String intpart = value.substring(0, value.indexOf("."));
    String decimalpart = value.substring(value.indexOf("."), value.length());
    //switch to double digit grouping
    formatter.applyPattern("#,##");
    returnValue.append(formatter.format(new BigDecimal(intpart).doubleValue() / 1000)).append(",");
    //appending last 3 digits and decimal part
    returnValue.append(intpart.substring(intpart.length() - 3, intpart.length())).append(decimalpart);
    //returning complete string
    if (returnValue.toString().equals(".00")) {
        return "0.00";
    }

    return returnValue.toString();

}

From source file:com.panet.imeta.core.util.StringUtil.java

public static double str2num(String pattern, String decimal, String grouping, String currency, String value)
        throws KettleValueException {
    // 0 : pattern
    // 1 : Decimal separator
    // 2 : Grouping separator
    // 3 : Currency symbol

    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat) nf;
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();

    if (!Const.isEmpty(pattern))
        df.applyPattern(pattern);
    if (!Const.isEmpty(decimal))
        dfs.setDecimalSeparator(decimal.charAt(0));
    if (!Const.isEmpty(grouping))
        dfs.setGroupingSeparator(grouping.charAt(0));
    if (!Const.isEmpty(currency))
        dfs.setCurrencySymbol(currency);
    try {//from   w  w  w  .j a v a 2 s  .  c  o m
        df.setDecimalFormatSymbols(dfs);
        return df.parse(value).doubleValue();
    } catch (Exception e) {
        String message = "Couldn't convert string to number " + e.toString();
        if (!isEmpty(pattern))
            message += " pattern=" + pattern;
        if (!isEmpty(decimal))
            message += " decimal=" + decimal;
        if (!isEmpty(grouping))
            message += " grouping=" + grouping.charAt(0);
        if (!isEmpty(currency))
            message += " currency=" + currency;
        throw new KettleValueException(message);
    }
}

From source file:org.openossad.util.core.util.StringUtil.java

public static double str2num(String pattern, String decimal, String grouping, String currency, String value)
        throws OpenDESIGNERValueException {
    // 0 : pattern
    // 1 : Decimal separator
    // 2 : Grouping separator
    // 3 : Currency symbol

    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat) nf;
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();

    if (!Const.isEmpty(pattern))
        df.applyPattern(pattern);
    if (!Const.isEmpty(decimal))
        dfs.setDecimalSeparator(decimal.charAt(0));
    if (!Const.isEmpty(grouping))
        dfs.setGroupingSeparator(grouping.charAt(0));
    if (!Const.isEmpty(currency))
        dfs.setCurrencySymbol(currency);
    try {//from   ww  w.ja  v  a 2s. c o m
        df.setDecimalFormatSymbols(dfs);
        return df.parse(value).doubleValue();
    } catch (Exception e) {
        String message = "Couldn't convert string to number " + e.toString();
        if (!isEmpty(pattern))
            message += " pattern=" + pattern;
        if (!isEmpty(decimal))
            message += " decimal=" + decimal;
        if (!isEmpty(grouping))
            message += " grouping=" + grouping.charAt(0);
        if (!isEmpty(currency))
            message += " currency=" + currency;
        throw new OpenDESIGNERValueException(message);
    }
}

From source file:com.wuliu.biz.util.WuliuWholeOrderDetailUtil.java

public static WuliuWholeOrderDetailModel builduliuWholeDetailModel(
        WuliuOrderDetailModel wuliuOrderDetailModel) {
    WuliuWholeOrderDetailModel ret = new WuliuWholeOrderDetailModel();
    ret.setCount(wuliuOrderDetailModel.getCount());
    ret.setHeight(wuliuOrderDetailModel.getHeight());
    ret.setId(wuliuOrderDetailModel.getId());
    ret.setLength(wuliuOrderDetailModel.getLength());
    ret.setMainOrderId(wuliuOrderDetailModel.getMainOrderId());
    ret.setStatus(wuliuOrderDetailModel.getStatus());
    ret.setWeight(wuliuOrderDetailModel.getWeight());
    ret.setWidth(wuliuOrderDetailModel.getWidth());
    ret.setTotalVolumn(wuliuOrderDetailModel.getHeight() * wuliuOrderDetailModel.getLength()
            * wuliuOrderDetailModel.getWidth() * wuliuOrderDetailModel.getCount());
    ret.setTotalWeight(wuliuOrderDetailModel.getWeight() * wuliuOrderDetailModel.getCount());

    DecimalFormat df = new DecimalFormat("0.#");
    if (ret.getLength() != null) {
        ret.setLengthForDisplay(df.format(ret.getLength() / 10.0));
    }/*from  w ww.  j  a va2  s  . c  o  m*/

    if (ret.getWidth() != null) {
        ret.setWidthForDisplay(df.format(ret.getWidth() / 10.0));
    }

    if (ret.getHeight() != null) {
        ret.setHeightForDisplay(df.format(ret.getHeight() / 10.0));
    }

    if (ret.getWeight() != null) {
        ret.setWeightForDisplay(df.format(ret.getWeight() / 1000.0));
    }

    df.applyPattern("0.###");
    if (ret.getTotalVolumn() != null) {
        if (ret.getTotalVolumn() % 1000000 == 0) {
            ret.setTotalVolumnForDisplay(df.format((ret.getTotalVolumn() / 1000000) / 1000.0));
        } else {
            ret.setTotalVolumnForDisplay(df.format(Math.ceil(ret.getTotalVolumn() / 1000000.0f) / 1000.0));
        }
    }

    df.applyPattern("0.#");
    if (ret.getTotalWeight() != null) {
        if (ret.getTotalWeight() % 1000 == 0) {
            ret.setTotalWeightForDisplay(String.valueOf(ret.getTotalWeight() / 1000));
        } else {
            ret.setTotalWeightForDisplay(df.format((ret.getTotalWeight() / 1000.0)));
        }
    }

    return ret;
}

From source file:org.revager.tools.GUITools.java

/**
 * Formats the given spinner.//  w ww. j  a  va 2 s . c  o  m
 * 
 * @param sp
 *            the spinner
 */
public static void formatSpinner(JSpinner sp, boolean hideBorder) {
    JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor) sp.getEditor();
    JFormattedTextField ftf = defEditor.getTextField();
    ftf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
    InternationalFormatter intFormatter = (InternationalFormatter) ftf.getFormatter();
    DecimalFormat decimalFormat = (DecimalFormat) intFormatter.getFormat();
    decimalFormat.applyPattern("00");
    DecimalFormatSymbols geSymbols = new DecimalFormatSymbols(Data.getInstance().getLocale());
    decimalFormat.setDecimalFormatSymbols(geSymbols);

    if (hideBorder) {
        sp.setBorder(null);
    }
}

From source file:org.pentaho.di.core.util.StringUtil.java

public static double str2num(String pattern, String decimal, String grouping, String currency, String value)
        throws KettleValueException {
    // 0 : pattern
    // 1 : Decimal separator
    // 2 : Grouping separator
    // 3 : Currency symbol

    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat) nf;
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();

    if (!Const.isEmpty(pattern)) {
        df.applyPattern(pattern);
    }/*from  w  w w  . j  ava2s.co  m*/
    if (!Const.isEmpty(decimal)) {
        dfs.setDecimalSeparator(decimal.charAt(0));
    }
    if (!Const.isEmpty(grouping)) {
        dfs.setGroupingSeparator(grouping.charAt(0));
    }
    if (!Const.isEmpty(currency)) {
        dfs.setCurrencySymbol(currency);
    }
    try {
        df.setDecimalFormatSymbols(dfs);
        return df.parse(value).doubleValue();
    } catch (Exception e) {
        String message = "Couldn't convert string to number " + e.toString();
        if (!isEmpty(pattern)) {
            message += " pattern=" + pattern;
        }
        if (!isEmpty(decimal)) {
            message += " decimal=" + decimal;
        }
        if (!isEmpty(grouping)) {
            message += " grouping=" + grouping.charAt(0);
        }
        if (!isEmpty(currency)) {
            message += " currency=" + currency;
        }
        throw new KettleValueException(message);
    }
}

From source file:com.webbfontaine.valuewebb.model.util.Utils.java

public static String getAsString(Number number, String pattern) {
    if (number == null) {
        return "";
    }/*from  w  ww.j  ava 2 s. c o m*/

    DecimalFormat decimalFormat = (DecimalFormat) NumberFormat
            .getNumberInstance(LocaleSelector.instance().getLocale());
    decimalFormat.applyPattern(pattern);
    return decimalFormat.format(number);
}