Example usage for java.text DecimalFormatSymbols getDecimalSeparator

List of usage examples for java.text DecimalFormatSymbols getDecimalSeparator

Introduction

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

Prototype

public char getDecimalSeparator() 

Source Link

Document

Gets the character used for decimal sign.

Usage

From source file:Main.java

private static void updateDecimalSeparator(DecimalFormat numberFormat) {
    DecimalFormatSymbols symbol = numberFormat.getDecimalFormatSymbols();
    if (symbol.getDecimalSeparator() != DECIMAL_SEPARATOR) {
        symbol.setDecimalSeparator(DECIMAL_SEPARATOR);
        numberFormat.setDecimalFormatSymbols(symbol);
    }/*from ww  w . j  a v a 2  s  .c o  m*/
}

From source file:Main.java

public static char setDecimalSeparator(Locale locale) {

    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
    return decimalFormatSymbols.getDecimalSeparator();

}

From source file:org.openestate.io.core.NumberUtils.java

/**
 * Test, if a string contains a parsable number.
 *
 * @param value/*from ww w.  j av  a 2  s.  c o  m*/
 * the value to check
 *
 * @param locale
 * the locale, against which the value is checked
 * (checks locale specific decimal and grouping separators)
 *
 * @return
 * true, if the provided value contains of numbers
 */
public static boolean isNumeric(String value, Locale locale) {
    if (value == null)
        return false;
    int start = 0;

    final DecimalFormatSymbols symbols = (locale != null) ? DecimalFormatSymbols.getInstance(locale)
            : DecimalFormatSymbols.getInstance();

    if (value.startsWith("+") || value.startsWith("-"))
        start++;
    boolean fraction = false;
    for (int i = start; i < value.length(); i++) {
        final char c = value.charAt(i);
        if (c == symbols.getDecimalSeparator() && !fraction) {
            fraction = true;
            continue;
        }
        //if (c==symbols.getGroupingSeparator() && !fraction)
        //{
        //  continue;
        //}
        if (!Character.isDigit(c)) {
            return false;
        }
    }
    return true;
}

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 a v  a2  s.c  om*/
    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.totschnig.myexpenses.util.Utils.java

public static char getDefaultDecimalSeparator() {
    char sep = '.';
    NumberFormat nfDLocal = NumberFormat.getNumberInstance();
    if (nfDLocal instanceof DecimalFormat) {
        DecimalFormatSymbols symbols = ((DecimalFormat) nfDLocal).getDecimalFormatSymbols();
        sep = symbols.getDecimalSeparator();
    }//from  w ww .  j a v a  2  s .c  o m
    return sep;
}

From source file:Currently.java

/**
 * Ignores any internal date format, and tries to show a complete
 * date/timp stamp in extended ISO 8601 format. UTC time (Zulu time)
 * or a local timezone will be used.<p>
 *
 * <table border=1>/*ww w.  j av  a  2  s .  c  o  m*/
 * <tr><th>zone</th><th>format</th><th>fraction</td><th>example</th></tr>
 * <tr><td>local</td><td>basic</td><td>integral</td><td>20020523T140427-0500</td></tr>
 * <tr><td>UTC</td><td>basic</td><td>integral</td><td>20020523190427Z</td></tr>
 * <tr><td>local</td><td>extd.</td><td>integral</td><td>2002-05-23T14:04:27-05:00</td></tr>
 * <tr><td>UTC</td><td>extd.</td><td>integral</td><td>2002-05-23T19:04:27Z</td></tr>
 * <tr><td>local</td><td>basic</td><td>millis</td><td>20020523T140427.166-0500</td></tr>
 * <tr><td>UTC</td><td>basic</td><td>millis</td><td>20020523190427.166Z</td></tr>
 * <tr><td>local</td><td>extd.</td><td>millis</td><td>2002-05-23T14:04:27.166-05:00</td></tr>
 * <tr><td>UTC</td><td>extd.</td><td>millis</td><td>2002-05-23T19:04:27.166Z</td></tr>
 * </table><p>
 *
 * @param zuluTime returns a UTC formatted stamp, if true. Otherwise
 * the time will be formatted according to the local zone. Local time
 * should be prefixed with the 'T'.
 * @param extendedFormat will use the extended ISO 8601 format which
 * separates the different timestamp items. If false, the basic
 * format will be used. In UTC and basic format, the 'T' separator
 * will be omitted.
 * @param withMillis will put the millisecond extension into the timestamp.
 * If false, the time will be without millisecond fraction. The separator
 * is taken from {@link java.text.DecimalFormatSymbols#getMinusSign()},
 * which usually is a period or a comma.
 * @param now is a time stamp as Date.
 * @return an ISO 8601 formatted date and time representation for
 * the given point in time.
 */
public static String iso8601(boolean zuluTime, boolean extendedFormat, boolean withMillis, Date now) {
    Calendar c = Calendar.getInstance(zuluTime ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault());
    c.setTime(now);

    // set up formattting options
    DecimalFormat nf2 = new DecimalFormat("##00");
    DecimalFormat nf3 = new DecimalFormat("###000");
    DecimalFormat nf4 = new DecimalFormat("####0000");
    DecimalFormatSymbols dfs = nf2.getDecimalFormatSymbols();

    // allocate result string buffer
    int size = extendedFormat ? (zuluTime ? 25 : 30) : (zuluTime ? 21 : 25);
    if (!withMillis) {
        size -= 4;
    }
    StringBuffer result = new StringBuffer(size);

    result.append(nf4.format(c.get(Calendar.YEAR)));
    if (extendedFormat) {
        result.append('-'); // position 5
    }
    result.append(nf2.format(c.get(Calendar.MONTH) + 1));
    if (extendedFormat) {
        result.append('-'); // position 8
    }
    result.append(nf2.format(c.get(Calendar.DAY_OF_MONTH)));
    // generating UTC in basic format may leave out the 'T' separator
    if (extendedFormat || !zuluTime) {
        result.append('T'); // position 11
    }
    result.append(nf2.format(c.get(Calendar.HOUR_OF_DAY)));
    if (extendedFormat) {
        result.append(':'); // position 14
    }
    result.append(nf2.format(c.get(Calendar.MINUTE)));
    if (extendedFormat) {
        result.append(':'); // position 17
    }
    result.append(nf2.format(c.get(Calendar.SECOND)));

    if (withMillis) {
        // Though there is no explicit spec which allows a complete
        // timestamp with milliseconds, it is implied through two
        // levels, sigh. 5.3.4.2 allows decimal fractions with
        // time-only stamps that have a timezone. The intro of 5.4.2
        // allows 5.3.1.3.
        result.append(dfs.getDecimalSeparator()); // position 20
        result.append(nf3.format(c.get(Calendar.MILLISECOND)));
    }

    if (zuluTime) {
        // this is easy
        result.append('Z');
    } else {
        // time zone calculations
        int zone_offset = c.get(Calendar.ZONE_OFFSET) / 1000;
        int save_offset = c.get(Calendar.DST_OFFSET) / 1000;
        int diff = (zone_offset + save_offset) / 60;
        result.append(diff < 0 ? dfs.getMinusSign() : '+'); // position 24

        if (diff < 0) {
            diff = Math.abs(diff);
        }
        result.append(nf2.format(diff / 60));
        if (extendedFormat) {
            result.append(':');
        }
        result.append(nf2.format(diff % 60));
    }

    return result.toString();
}

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());
    }//from   w  w w  . j a v a 2  s . c  o  m
}

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();/*  ww  w .  j ava2  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 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();/* w  ww  .  jav  a 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 {
        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);
    }
}