Given a string, determine if it is an int, a float or neither.
Examples
- 123 - int
- 62727.0033 - float
- 644c.33 - neither
Your code should output a different value depending on which it is. For example it could return "i" for int, "f" for float and "n" for neither.
Details
The following are floats:
1.00 -0.0 1.0
The following are not floats:
1e3 1.0ec3 -1e3 1e-3 1e+3 --1.0 +1.0 NaN 1.0.0 1. .1 -001.1
The following are ints:
42 -2 -0
The following are not ints:
042 00
In short, the rule for floats is that it is a sequence of digits following by a .
followed by a sequence of digits. Optionally -
is prepended. If it starts with 0
then the .
must follow directly afterwards.
The rule for ints is that it is a sequence of digits where the first digit is not 0
. The only exception is that 0
and -0
are ints.
Either sort of number can have -
optionally prepended to it.
You can assume the input will only contain printable-ASCII.
+1.0
not a float in ur mind ?"sequence of digits following by a . followed by a sequence of digits"
- this satisfies the rule. while+
is optional, and most don't include it, that doesn't make this number any less valid of a float. \$\endgroup\$echo '+1.0' | awk '$++NF = exp($1)' ::::::::::::::::::::::::::::: :::::::::::::::::::::::::::: ::::::::::::::: +1.0 2.71828
. :::::::::::::::::::::::::::: :::::::::::::::::::::::: :::::::::::::::::::::::: Even though its effects are transparent by and large, any parser that fails to properly recognize and parse a positive sign, is not worth its salt. \$\endgroup\$echo '+1.0' | python3 -c 'import sys, math; [ print(math.exp(float(_))) for _ in sys.stdin ]' 2.718281828459045
\$\endgroup\$