Java Number Parse isNumeric(String value, Locale locale)

Here you can find the source of isNumeric(String value, Locale locale)

Description

Test, if a string contains a parsable number.

License

Apache License

Parameter

Parameter Description
value the value to check
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

Declaration

public static boolean isNumeric(String value, Locale locale) 

Method Source Code


//package com.java2s;
/*//ww  w.  ja  v a  2s .  c  om
 * Copyright 2015-2016 OpenEstate.org.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DecimalFormatSymbols;

import java.util.Locale;

public class Main {
    /**
     * Test, if a string contains a parsable number.
     *
     * @param value
     * 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;
    }
}

Related

  1. isNumeric(Class cls)
  2. isNumeric(final String str)
  3. isNumeric(String str)
  4. isNumeric(String str)
  5. isNumeric(String str)
  6. isParsable(Object parser, String str)
  7. toNumber(char letter)
  8. toNumber(CharSequence jsonText)
  9. toNumber(final Object obj)