[Leetcode]-valid number (valid number)

Source: Internet
Author: User

[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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.