[Problem:]
Validate if a given string is Numeric. judge whether the given string is a numerical value.
Some examples:
"0" => true
"0.1" => true
"ABC" => false
"1 A" => false
"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You shoshould gather all requirements up front before implementing one.
[Solution:]
public class Solution { public static boolean isNumber(String s) { try { s = s.toLowerCase(); Double.parseDouble(s); } catch (Exception e) { return false; } return s.charAt(s.length() -1) != 'f' && s.charAt(s.length() - 1) != 'd'; }}
Note: s cannot end with 'f' or 'D', because if they end with double conversion, no error is returned,
However, for example: 10.1f and ike1d cannot be counted as valid values based on the meaning of the question. Therefore, we cannot let go of these "fish without a net ".
[Expansion:]
1. Float: Single-precision floating point number
When declaring a float-Type Floating Point Number, add f or f at the end.
Float F1 = 11.11f; // Positive floating point number
Float F2 =-17.15f; // negative floating point number
2. Double: double-precision floating point number
When declared as a floating point of the double type, you can add D or D at the end. Of course, this parameter is not required because the default floating point type is double.
Double d1 = 11.11223d; // Positive floating point number
Double D2 = 11.11333d; // Positive floating point number
Double D3 =-17.15555; // negative floating point number
** We recommend that you add D or D to the number of double data types to distinguish them from single-precision floating-point numbers.
[Leetcode]-valid number (valid number)