Here you can find the source of isDouble(String _str)
Parameter | Description |
---|---|
_str | string to check |
public static boolean isDouble(String _str)
//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); } }