Example usage for java.text DecimalFormatSymbols getGroupingSeparator

List of usage examples for java.text DecimalFormatSymbols getGroupingSeparator

Introduction

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

Prototype

public char getGroupingSeparator() 

Source Link

Document

Gets the character used for thousands separator.

Usage

From source file:com.xpfriend.fixture.cast.temp.TypeConverter.java

private static String removeGroupingSeparator(String text) {
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
    char groupingSeparator = symbols.getGroupingSeparator();
    StringBuilder sb = new StringBuilder(text.length());
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        if (c != groupingSeparator) {
            sb.append(c);//from  w  w w. ja v a 2s . c  om
        }
    }
    return sb.toString();
}

From source file:nl.strohalm.cyclos.utils.conversion.UnitsConverter.java

public UnitsConverter(final String pattern, final DecimalFormat numberFormat) {
    super(BigDecimal.class, numberFormat);
    this.pattern = StringUtils.replace(pattern, "#amount#", "{0}");
    final DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols();
    decimalSeparator = symbols.getDecimalSeparator();
    groupingSeparator = symbols.getGroupingSeparator();
}

From source file:nl.strohalm.cyclos.utils.conversion.NumberConverter.java

public T valueOf(String string) {
    if (StringUtils.isEmpty(string)) {
        return null;
    }/*from   w  w  w .j av  a 2  s  . co m*/
    final DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols();
    final char minusSign = symbols.getMinusSign();
    final char decimalSeparator = symbols.getDecimalSeparator();
    final char groupingSeparator = symbols.getGroupingSeparator();
    boolean negativeNumber = false;
    if (string.indexOf(minusSign) > -1) {
        string = StringUtils.replace(string, String.valueOf(minusSign), "");
        negativeNumber = true;
    }
    final String[] parts = StringUtils.split(string, String.valueOf(decimalSeparator));
    final String integerPart = StringUtils.replace(parts[0], String.valueOf(groupingSeparator), "");
    final boolean hasDecimalPart = parts.length > 1;
    final String decimalPart = hasDecimalPart ? parts[1] : "";
    String bigDecimalString = integerPart;
    if (hasDecimalPart) {
        bigDecimalString = bigDecimalString + "." + decimalPart;
    }
    if (negativeNumber) {
        bigDecimalString = "-" + bigDecimalString;
    }
    final BigDecimal bigDecimal = new BigDecimal(bigDecimalString);
    T value = CoercionHelper.coerce(numberType, bigDecimal);
    if (negativeToAbsoluteValue && value != null && value.floatValue() < 0) {
        value = CoercionHelper.coerce(numberType, -value.floatValue());
    }
    return value;
}

From source file:org.mifosplatform.infrastructure.core.serialization.JsonParserHelper.java

public BigDecimal convertFrom(final String numericalValueFormatted, final String parameterName,
        final Locale clientApplicationLocale) {

    if (clientApplicationLocale == null) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
        final String defaultMessage = new StringBuilder(
                "The parameter '" + parameterName + "' requires a 'locale' parameter to be passed with it.")
                        .toString();/*ww  w .  j  a  va  2 s.c om*/
        final ApiParameterError error = ApiParameterError
                .parameterError("validation.msg.missing.locale.parameter", defaultMessage, parameterName);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }

    try {
        BigDecimal number = null;

        if (StringUtils.isNotBlank(numericalValueFormatted)) {

            String source = numericalValueFormatted.trim();

            final NumberFormat format = NumberFormat.getNumberInstance(clientApplicationLocale);
            final DecimalFormat df = (DecimalFormat) format;
            final DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
            // http://bugs.sun.com/view_bug.do?bug_id=4510618
            final char groupingSeparator = symbols.getGroupingSeparator();
            if (groupingSeparator == '\u00a0') {
                source = source.replaceAll(" ", Character.toString('\u00a0'));
            }

            final NumberFormatter numberFormatter = new NumberFormatter();
            final Number parsedNumber = numberFormatter.parse(source, clientApplicationLocale);
            number = BigDecimal.valueOf(Double.valueOf(parsedNumber.doubleValue()));
        }

        return number;
    } catch (final ParseException e) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
        final ApiParameterError error = ApiParameterError.parameterError(
                "validation.msg.invalid.decimal.format",
                "The parameter " + parameterName + " has value: " + numericalValueFormatted
                        + " which is invalid decimal value for provided locale of ["
                        + clientApplicationLocale.toString() + "].",
                parameterName, numericalValueFormatted, clientApplicationLocale);
        error.setValue(numericalValueFormatted);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }
}

From source file:org.mifosplatform.infrastructure.core.serialization.JsonParserHelper.java

public Integer convertToInteger(final String numericalValueFormatted, final String parameterName,
        final Locale clientApplicationLocale) {

    if (clientApplicationLocale == null) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
        final String defaultMessage = new StringBuilder(
                "The parameter '" + parameterName + "' requires a 'locale' parameter to be passed with it.")
                        .toString();//www  .  j  av  a 2  s  . co  m
        final ApiParameterError error = ApiParameterError
                .parameterError("validation.msg.missing.locale.parameter", defaultMessage, parameterName);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }

    try {
        Integer number = null;

        if (StringUtils.isNotBlank(numericalValueFormatted)) {

            String source = numericalValueFormatted.trim();

            final NumberFormat format = NumberFormat.getInstance(clientApplicationLocale);
            final DecimalFormat df = (DecimalFormat) format;
            final DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
            df.setParseBigDecimal(true);

            // http://bugs.sun.com/view_bug.do?bug_id=4510618
            final char groupingSeparator = symbols.getGroupingSeparator();
            if (groupingSeparator == '\u00a0') {
                source = source.replaceAll(" ", Character.toString('\u00a0'));
            }

            final Number parsedNumber = df.parse(source);

            final double parsedNumberDouble = parsedNumber.doubleValue();
            final int parsedNumberInteger = parsedNumber.intValue();

            if (source.contains(Character.toString(symbols.getDecimalSeparator()))) {
                throw new ParseException(source, 0);
            }

            if (!Double.valueOf(parsedNumberDouble)
                    .equals(Double.valueOf(Integer.valueOf(parsedNumberInteger)))) {
                throw new ParseException(source, 0);
            }

            number = parsedNumber.intValue();
        }

        return number;
    } catch (final ParseException e) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
        final ApiParameterError error = ApiParameterError.parameterError(
                "validation.msg.invalid.integer.format",
                "The parameter " + parameterName + " has value: " + numericalValueFormatted
                        + " which is invalid integer value for provided locale of ["
                        + clientApplicationLocale.toString() + "].",
                parameterName, numericalValueFormatted, clientApplicationLocale);
        error.setValue(numericalValueFormatted);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

public BigDecimal convertFrom(final String numericalValueFormatted, final String parameterName,
        final Locale clientApplicationLocale) {

    if (clientApplicationLocale == null) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
        final String defaultMessage = new StringBuilder(
                "The parameter '" + parameterName + "' requires a 'locale' parameter to be passed with it.")
                        .toString();//w  w  w .  j a  va 2s  . c  o m
        final ApiParameterError error = ApiParameterError
                .parameterError("validation.msg.missing.locale.parameter", defaultMessage, parameterName);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }

    try {
        BigDecimal number = null;

        if (StringUtils.isNotBlank(numericalValueFormatted)) {

            String source = numericalValueFormatted.trim();

            final NumberFormat format = NumberFormat.getNumberInstance(clientApplicationLocale);
            final DecimalFormat df = (DecimalFormat) format;
            final DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
            // http://bugs.sun.com/view_bug.do?bug_id=4510618
            final char groupingSeparator = symbols.getGroupingSeparator();
            if (groupingSeparator == '\u00a0') {
                source = source.replaceAll(" ", Character.toString('\u00a0'));
            }

            final NumberFormatter numberFormatter = new NumberFormatter();
            final Number parsedNumber = numberFormatter.parse(source, clientApplicationLocale);
            if (parsedNumber instanceof BigDecimal) {
                number = (BigDecimal) parsedNumber;
            } else {
                number = BigDecimal.valueOf(Double.valueOf(parsedNumber.doubleValue()));
            }
        }

        return number;
    } catch (final ParseException e) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
        final ApiParameterError error = ApiParameterError.parameterError(
                "validation.msg.invalid.decimal.format",
                "The parameter " + parameterName + " has value: " + numericalValueFormatted
                        + " which is invalid decimal value for provided locale of ["
                        + clientApplicationLocale.toString() + "].",
                parameterName, numericalValueFormatted, clientApplicationLocale);
        error.setValue(numericalValueFormatted);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }
}

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

public Integer convertToInteger(final String numericalValueFormatted, final String parameterName,
        final Locale clientApplicationLocale) {

    if (clientApplicationLocale == null) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
        final String defaultMessage = new StringBuilder(
                "The parameter '" + parameterName + "' requires a 'locale' parameter to be passed with it.")
                        .toString();/*from  www .j a  va2  s  .c om*/
        final ApiParameterError error = ApiParameterError
                .parameterError("validation.msg.missing.locale.parameter", defaultMessage, parameterName);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }

    try {
        Integer number = null;

        if (StringUtils.isNotBlank(numericalValueFormatted)) {

            String source = numericalValueFormatted.trim();

            final NumberFormat format = NumberFormat.getInstance(clientApplicationLocale);
            final DecimalFormat df = (DecimalFormat) format;
            final DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
            df.setParseBigDecimal(true);

            // http://bugs.sun.com/view_bug.do?bug_id=4510618
            final char groupingSeparator = symbols.getGroupingSeparator();
            if (groupingSeparator == '\u00a0') {
                source = source.replaceAll(" ", Character.toString('\u00a0'));
            }

            final Number parsedNumber = df.parse(source);

            final double parsedNumberDouble = parsedNumber.doubleValue();
            final int parsedNumberInteger = parsedNumber.intValue();

            if (source.contains(Character.toString(symbols.getDecimalSeparator()))) {
                throw new ParseException(source, 0);
            }

            if (!Double.valueOf(parsedNumberDouble)
                    .equals(Double.valueOf(Integer.valueOf(parsedNumberInteger)))) {
                throw new ParseException(source, 0);
            }

            number = parsedNumber.intValue();
        }

        return number;
    } catch (final ParseException e) {

        final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
        final ApiParameterError error = ApiParameterError.parameterError(
                "validation.msg.invalid.integer.format",
                "The parameter " + parameterName + " has value: " + numericalValueFormatted
                        + " which is invalid integer value for provided locale of ["
                        + clientApplicationLocale.toString() + "].",
                parameterName, numericalValueFormatted, clientApplicationLocale);
        error.setValue(numericalValueFormatted);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }
}

From source file:org.mozilla.gecko.GeckoAppShell.java

private static void putLocaleEnv() {
    GeckoAppShell.putenv("LANG=" + Locale.getDefault().toString());
    NumberFormat nf = NumberFormat.getInstance();
    if (nf instanceof DecimalFormat) {
        DecimalFormat df = (DecimalFormat) nf;
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();

        GeckoAppShell.putenv("LOCALE_DECIMAL_POINT=" + dfs.getDecimalSeparator());
        GeckoAppShell.putenv("LOCALE_THOUSANDS_SEP=" + dfs.getGroupingSeparator());
        GeckoAppShell.putenv("LOCALE_GROUPING=" + (char) df.getGroupingSize());
    }//  ww w  .  ja  va2  s  .c om
}

From source file:org.orcid.frontend.web.controllers.FundingsController.java

/**
 * Transforms a string into a BigDecimal
 * /*from w w  w .  j a  v  a2  s  .  co m*/
 * @param amount
 * @param locale
 * @return a BigDecimal containing the given amount
 * @throws Exception
 *             if the amount cannot be correctly parse into a BigDecimal
 * */
public BigDecimal getAmountAsBigDecimal(String amount, Locale locale) throws Exception {
    try {
        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);
        if (number == null || parsePosition.getIndex() != amount.length()) {
            throw new Exception();
        }
        return new BigDecimal(number.toString());
    } catch (Exception e) {
        throw e;
    }
}

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

/**
 * Replace the funding amount string into the desired format
 * /*from ww  w  .  ja va2  s .c  o 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);
        }
    }
}