Example usage for java.text DecimalFormatSymbols setCurrencySymbol

List of usage examples for java.text DecimalFormatSymbols setCurrencySymbol

Introduction

In this page you can find the example usage for java.text DecimalFormatSymbols setCurrencySymbol.

Prototype

public void setCurrencySymbol(String currency) 

Source Link

Document

Sets the currency symbol for the currency of these DecimalFormatSymbols in their locale.

Usage

From source file:MainClass.java

public static void main(String[] argv) {
    NumberFormat nf = NumberFormat.getInstance();
    if (nf instanceof DecimalFormat) {
        DecimalFormat df = (DecimalFormat) nf;

        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();

        dfs.setCurrencySymbol("AD$");

        df.setDecimalFormatSymbols(dfs);

        df.applyPattern("\u00a4#,###.##");
    }/*from w  ww  .  ja va2  s.  com*/

    System.out.println(nf.format(1234.56));

}

From source file:Main.java

public static String safeDoubleToCurrency(Double val) {
    BigDecimal value = BigDecimal.valueOf(val);
    DecimalFormat kursIndonesia = (DecimalFormat) DecimalFormat.getCurrencyInstance();
    DecimalFormatSymbols formatRp = new DecimalFormatSymbols();

    formatRp.setCurrencySymbol("");
    formatRp.setMonetaryDecimalSeparator('.');
    formatRp.setGroupingSeparator(',');

    kursIndonesia.setDecimalFormatSymbols(formatRp);
    kursIndonesia.setParseBigDecimal(true);
    if (val < 1)
        kursIndonesia.setMaximumFractionDigits(8);
    else if (val < 10)
        kursIndonesia.setMaximumFractionDigits(6);
    else if (val < 100)
        kursIndonesia.setMaximumFractionDigits(4);

    return kursIndonesia.format(value);
}

From source file:Main.java

public static boolean isRTL(Locale locale, String symbol) {
    NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);

    // We then tell our formatter to use this symbol.
    DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) currencyFormat)
            .getDecimalFormatSymbols();/* ww w. j  a v  a  2s  . c om*/
    decimalFormatSymbols.setCurrencySymbol(symbol);
    ((java.text.DecimalFormat) currencyFormat).setDecimalFormatSymbols(decimalFormatSymbols);

    String formattedtext = currencyFormat.format(100.0);

    if (formattedtext.startsWith(symbol)) {
        return false;
    } else {
        return true;
    }

    /*
    final int directionality = Character.getDirectionality(String.format(locale,"%s",locale.getDisplayLanguage(locale)).charAt(0));
            
    if ( (directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT) ||
        (directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) )
    {
    return true;
    }
    else
    {
    return false;
    }
     */
}

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);//from ww w  .j a v  a  2 s  .com
    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.ocs.dynamo.ui.converter.CurrencyBigDecimalConverter.java

@Override
protected DecimalFormat constructFormat(Locale locale) {
    // ignore the locale that is passed as a parameter, and use the default
    // locale instead so
    // that the number formatting is always the same
    DecimalFormat nf = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
    DecimalFormatSymbols s = nf.getDecimalFormatSymbols();
    s.setCurrencySymbol(currencySymbol);
    nf.setDecimalFormatSymbols(s);//  w  w  w.  j  a  v  a  2  s.  c  om
    return nf;
}

From source file:be.ibridge.kettle.trans.step.textfileinput.TextFileInput.java

public static final Value convertValue(String pol, String field_name, int field_type, String field_format,
        int field_length, int field_precision, String num_group, String num_decimal, String num_currency,
        String nullif, String ifNull, int trim_type, DecimalFormat ldf, DecimalFormatSymbols ldfs,
        SimpleDateFormat ldaf, DateFormatSymbols ldafs) throws Exception {
    Value value = new Value(field_name, field_type); // build a value!

    // If no nullif field is supplied, take the default.
    String null_value = nullif;/*  w w w  . ja va  2s. c  o  m*/
    if (null_value == null) {
        switch (field_type) {
        case Value.VALUE_TYPE_BOOLEAN:
            null_value = Const.NULL_BOOLEAN;
            break;
        case Value.VALUE_TYPE_STRING:
            null_value = Const.NULL_STRING;
            break;
        case Value.VALUE_TYPE_BIGNUMBER:
            null_value = Const.NULL_BIGNUMBER;
            break;
        case Value.VALUE_TYPE_NUMBER:
            null_value = Const.NULL_NUMBER;
            break;
        case Value.VALUE_TYPE_INTEGER:
            null_value = Const.NULL_INTEGER;
            break;
        case Value.VALUE_TYPE_DATE:
            null_value = Const.NULL_DATE;
            break;
        case Value.VALUE_TYPE_BINARY:
            null_value = Const.NULL_BINARY;
            break;
        default:
            null_value = Const.NULL_NONE;
            break;
        }
    }
    String null_cmp = Const.rightPad(new StringBuffer(null_value), pol.length());

    if (pol == null || pol.length() == 0 || pol.equalsIgnoreCase(null_cmp)) {
        if (ifNull != null && ifNull.length() != 0)
            pol = ifNull;
    }

    if (pol == null || pol.length() == 0 || pol.equalsIgnoreCase(null_cmp)) {
        value.setNull();
    } else {
        if (value.isNumeric()) {
            try {
                StringBuffer strpol = new StringBuffer(pol);

                switch (trim_type) {
                case TextFileInputMeta.TYPE_TRIM_LEFT:
                    while (strpol.length() > 0 && strpol.charAt(0) == ' ')
                        strpol.deleteCharAt(0);
                    break;
                case TextFileInputMeta.TYPE_TRIM_RIGHT:
                    while (strpol.length() > 0 && strpol.charAt(strpol.length() - 1) == ' ')
                        strpol.deleteCharAt(strpol.length() - 1);
                    break;
                case TextFileInputMeta.TYPE_TRIM_BOTH:
                    while (strpol.length() > 0 && strpol.charAt(0) == ' ')
                        strpol.deleteCharAt(0);
                    while (strpol.length() > 0 && strpol.charAt(strpol.length() - 1) == ' ')
                        strpol.deleteCharAt(strpol.length() - 1);
                    break;
                default:
                    break;
                }
                pol = strpol.toString();

                if (value.isNumber()) {
                    if (field_format != null) {
                        ldf.applyPattern(field_format);

                        if (num_decimal != null && num_decimal.length() >= 1)
                            ldfs.setDecimalSeparator(num_decimal.charAt(0));
                        if (num_group != null && num_group.length() >= 1)
                            ldfs.setGroupingSeparator(num_group.charAt(0));
                        if (num_currency != null && num_currency.length() >= 1)
                            ldfs.setCurrencySymbol(num_currency);

                        ldf.setDecimalFormatSymbols(ldfs);
                    }

                    value.setValue(ldf.parse(pol).doubleValue());
                } else {
                    if (value.isInteger()) {
                        value.setValue(Long.parseLong(pol));
                    } else {
                        if (value.isBigNumber()) {
                            value.setValue(new BigDecimal(pol));
                        } else {
                            throw new KettleValueException("Unknown numeric type: contact vendor!");
                        }
                    }
                }
            } catch (Exception e) {
                throw (e);
            }
        } else {
            if (value.isString()) {
                value.setValue(pol);
                switch (trim_type) {
                case TextFileInputMeta.TYPE_TRIM_LEFT:
                    value.ltrim();
                    break;
                case TextFileInputMeta.TYPE_TRIM_RIGHT:
                    value.rtrim();
                    break;
                case TextFileInputMeta.TYPE_TRIM_BOTH:
                    value.trim();
                    break;
                default:
                    break;
                }
                if (pol.length() == 0)
                    value.setNull();
            } else {
                if (value.isDate()) {
                    try {
                        if (field_format != null) {
                            ldaf.applyPattern(field_format);
                            ldaf.setDateFormatSymbols(ldafs);
                        }

                        value.setValue(ldaf.parse(pol));
                    } catch (Exception e) {
                        throw (e);
                    }
                } else {
                    if (value.isBinary()) {
                        value.setValue(pol.getBytes());
                    }
                }
            }
        }
    }

    value.setLength(field_length, field_precision);

    return value;
}

From source file:org.apache.cocoon.template.instruction.FormatNumber.java

private void setCurrency(NumberFormat formatter, String currencyCode, String currencySymbol) throws Exception {
    String code = null;/* w w  w  .  ja  va 2 s .co m*/
    String symbol = null;

    if (currencyCode == null) {
        if (currencySymbol == null) {
            return;
        }
        symbol = currencySymbol;
    } else if (currencySymbol != null) {
        if (currencyClass != null) {
            code = currencyCode;
        } else {
            symbol = currencySymbol;
        }
    } else if (currencyClass != null) {
        code = currencyCode;
    } else {
        symbol = currencyCode;
    }
    if (code != null) {
        Object[] methodArgs = new Object[1];

        /*
         * java.util.Currency.getInstance()
         */
        Method m = currencyClass.getMethod("getInstance", new Class[] { String.class });

        methodArgs[0] = code;
        Object currency = m.invoke(null, methodArgs);

        /*
         * java.text.NumberFormat.setCurrency()
         */
        Class[] paramTypes = new Class[1];
        paramTypes[0] = currencyClass;
        Class numberFormatClass = Class.forName("java.text.NumberFormat");
        m = numberFormatClass.getMethod("setCurrency", paramTypes);
        methodArgs[0] = currency;
        m.invoke(formatter, methodArgs);
    } else {
        /*
         * Let potential ClassCastException propagate up (will almost never
         * happen)
         */
        DecimalFormat df = (DecimalFormat) formatter;
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        dfs.setCurrencySymbol(symbol);
        df.setDecimalFormatSymbols(dfs);
    }
}

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);//from ww w  . java2  s.  c  o  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 OpenDESIGNERValueException(message);
    }
}

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);//  w ww  . j av  a 2  s . c  om
    }
    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);
    }
}