Java Decimal Format isDouble(String _str)

Here you can find the source of isDouble(String _str)

Description

Checks if the given string is a valid double (including negative values).

License

Open Source License

Parameter

Parameter Description
_str string to check

Return

true if valid double, false otherwise

Declaration

public static boolean isDouble(String _str) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormatSymbols;

public class Main {
    /**// w w  w . j a va2 s  . co  m
     * Checks if the given string is a valid double (including negative values).
     * Separator is based on the used locale.
     * @param _str string to check
     * @return true if valid double, false otherwise
     */
    public static boolean isDouble(String _str) {
        return isDouble(_str, true);
    }

    /**
     * Checks if the given string is a valid double (including negative values) using the given separator.
     * @param _str string to check
     * @param _separator separator to use
     * @return true if valid double, false otherwise
     */
    public static boolean isDouble(String _str, char _separator) {
        return isDouble(_str, _separator, true);
    }

    /**
     * Checks if the given string is a valid double.
     * The used separator is based on the used system locale
     * @param _str string to validate
     * @param _allowNegative set to true if negative double should be allowed
     * @return true if given string is double, false otherwise
     */
    public static boolean isDouble(String _str, boolean _allowNegative) {
        return isDouble(_str, DecimalFormatSymbols.getInstance().getDecimalSeparator(), _allowNegative);
    }

    /**
     * Checks if the given string is a valid double.
     * @param _str string to validate
     * @param _separator seperator used (e.g. "." (dot) or "," (comma))
     * @param _allowNegative set to true if negative double should be allowed
     * @return true if given string is double, false otherwise
     */
    public static boolean isDouble(String _str, char _separator, boolean _allowNegative) {
        String pattern = "\\d+XXX\\d+$";
        if (_separator == '.') {
            pattern = pattern.replace("XXX", "\\.?");
        } else {
            pattern = pattern.replace("XXX", _separator + "?");
        }

        if (_allowNegative) {
            pattern = "^-?" + pattern;
        } else {
            pattern = "^" + pattern;
        }

        if (_str != null) {
            return _str.matches(pattern) || isInteger(_str, _allowNegative);
        }
        return false;
    }

    /**
     * Check if string is integer (including negative integers).
     *
     * @param _str string to check
     * @return true if integer (positive or negative), false otherwise
     */
    public static boolean isInteger(String _str) {
        return isInteger(_str, true);
    }

    /**
     * Check if string is an either positive or negative integer.
     *
     * @param _str string to validate
     * @param _allowNegative negative integer allowed
     * @return true if integer, false otherwise
     */
    public static boolean isInteger(String _str, boolean _allowNegative) {
        if (_str == null) {
            return false;
        }

        String regex = "[0-9]+$";
        if (_allowNegative) {
            regex = "^-?" + regex;
        } else {
            regex = "^" + regex;
        }
        return _str.matches(regex);
    }
}

Related

  1. getString(Double d)
  2. getStringFromDouble(double number)
  3. getStringRepresentationForDouble(double value)
  4. getTwoPoint(double val)
  5. getValue(double value)
  6. isLikelyDouble(long value)
  7. isprime(double x)
  8. isValidDouble(String value, int decimals)
  9. Julian_Cal(double JDN)