Example usage for java.text DecimalFormat setDecimalFormatSymbols

List of usage examples for java.text DecimalFormat setDecimalFormatSymbols

Introduction

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

Prototype

public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) 

Source Link

Document

Sets the decimal format symbols, which is generally not changed by the programmer or user.

Usage

From source file:org.orcid.core.manager.impl.OrcidProfileManagerImpl.java

/**
 * Replace the funding amount string into the desired format
 * //from ww  w . j av a 2 s  . co m
 * @param updatedOrcidProfile
 *            The profile containing the new funding
 * */
private void setFundingAmountsWithTheCorrectFormat(OrcidProfile updatedOrcidProfile)
        throws IllegalArgumentException {
    FundingList fundings = updatedOrcidProfile.retrieveFundings();

    for (Funding funding : fundings.getFundings()) {
        // If the amount is not empty, update it
        if (funding.getAmount() != null && StringUtils.isNotBlank(funding.getAmount().getContent())) {
            String amount = funding.getAmount().getContent();
            Locale locale = localeManager.getLocale();
            ParsePosition parsePosition = new ParsePosition(0);
            DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
            DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols();
            /**
             * When spaces are allowed, the grouping separator is the
             * character 160, which is a non-breaking space So, lets change
             * it so it uses the default space as a separator
             * */
            if (symbols.getGroupingSeparator() == 160) {
                symbols.setGroupingSeparator(' ');
            }
            numberFormat.setDecimalFormatSymbols(symbols);
            Number number = numberFormat.parse(amount, parsePosition);
            String formattedAmount = number.toString();
            if (parsePosition.getIndex() != amount.length()) {
                double example = 1234567.89;
                NumberFormat numberFormatExample = NumberFormat.getNumberInstance(localeManager.getLocale());
                throw new IllegalArgumentException(
                        "The amount: " + amount + " doesn'n have the right format, it should use the format: "
                                + numberFormatExample.format(example));
            }
            funding.getAmount().setContent(formattedAmount);
        }
    }
}

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;//  ww  w.  ja va2  s  . com
    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.sakaiproject.tool.assessment.facade.AssessmentGradingFacadeQueries.java

/***
 * /*from   w w  w .  java 2 s .c  o m*/
 *@author Mustansar Mehmood
 */
public Double getAverageSubmittedAssessmentGrading(final Long publishedAssessmentId, final String agentId) {
    Double averageScore = new Double(0.0);
    AssessmentGradingData ag = null;
    final String query = "from AssessmentGradingData a "
            + " where a.publishedAssessmentId=? and a.agentId=? and "
            + " a.forGrade=?  order by  a.submittedDate desc";

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.createQuery(query);
            q.setLong(0, publishedAssessmentId.longValue());
            q.setString(1, agentId);
            q.setBoolean(2, true);
            return q.list();
        };
    };
    List assessmentGradings = getHibernateTemplate().executeFind(hcb);

    if (assessmentGradings.size() != 0) {
        AssessmentGradingData agd = null;
        Double cumulativeScore = new Double(0);
        Iterator i = assessmentGradings.iterator();

        while (i.hasNext()) {
            agd = (AssessmentGradingData) i.next();
            cumulativeScore += agd.getFinalScore();
        }
        averageScore = cumulativeScore / assessmentGradings.size();

        DecimalFormat df = new DecimalFormat("0.##");
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
        dfs.setDecimalSeparator('.');
        df.setDecimalFormatSymbols(dfs);

        averageScore = new Double(df.format((double) averageScore));
    }
    return averageScore;
}