Restricted Decimal Number Validator
Problem statement
You are given a string toTest. Return true exactly when it represents a number in the restricted decimal grammar below.
- An optional minus sign
-may appear only as the first character. - At least one ASCII digit must follow the optional sign.
- An optional decimal point
.may appear at most once, and it must have at least one digit on each side. - No other characters, whitespace, exponent notation, or leading plus sign are accepted.
Leading zeros are allowed.
Function
isNumber(toTest: String) → booleanExamples
Example 1
toTest = "234"return = trueEvery character is an ASCII digit.
Example 2
toTest = "-455"return = trueA single leading minus sign is allowed.
Example 3
toTest = "1e2"return = falseExponent notation is outside the accepted decimal grammar.
Example 4
toTest = "12^2"return = falseThe caret is not a digit or the optional decimal point.
Example 5
toTest = "23.23"return = trueThere are digits on both sides of the only decimal point.
Example 6
toTest = "2.2.4"return = falseA valid representation may contain at most one decimal point.
Constraints
0 ≤ toTest.length ≤ 100000toTestconsists of printable ASCII characters.