I'm hoping there's something in the same conceptual space as the old VB6 IsNumeric()
function?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I recently wrote an article about ways to ensure a variable is a valid number: https://github.com/jehugaleahsa/artifacts/blob/master/2018/typescript_num_hack.md The article explains how to ensure floating point or integer, if that's important (
+x
vs~~x
).The article assumes the variable is a
string
or anumber
to begin with andtrim
is available/polyfilled. It wouldn't be hard to extend it to handle other types, as well. Here's the meat of it:My attempt at a slightly confusing, Pherhaps not the best solution
To check if a variable (including a string) is a number, check if it is not a number:
This works regardless of whether the variable contains is a string or number.
Examples
Of course, you can negate this if you need to. For example, to implement the
IsNumeric
example you gave:To convert a string containing a number into a number:
only works if the string only contains numeric characters, else it returns
NaN
.Examples
To convert a string loosely to a number
useful for converting '12px' to 12, for example:
Examples
Floats
Bear in mind that, unlike
+num
,parseInt
(as the name suggests) will convert a float into an integer by chopping off everything following the decimal point (if you want to useparseInt()
because of this behaviour, you're probably better off using another method instead):Empty strings
Empty strings may be a little counter-intuitive.
+num
converts empty strings to zero, andisNaN()
assumes the same:But
parseInt()
does not agree:If anyone ever gets this far down, I spent some time hacking on this trying to patch moment.js (https://github.com/moment/moment). Here's something that I took away from it:
Handles the following cases:
True! :
False! :
Ironically, the one I am struggling with the most:
Any suggestions welcome. :]
And you could go the RegExp-way:
If you really want to make sure that a string contains only a number, any number (integer or floating point), and exactly a number, you cannot use
parseInt()
/parseFloat()
,Number()
, or!isNaN()
by themselves. Note that!isNaN()
is actually returningtrue
whenNumber()
would return a number, andfalse
when it would returnNaN
, so I will exclude it from the rest of the discussion.The problem with
parseFloat()
is that it will return a number if the string contains any number, even if the string doesn't contain only and exactly a number:The problem with
Number()
is that it will return a number in cases where the passed value is not a number at all!The problem with rolling your own regex is that unless you create the exact regex for matching a floating point number as Javascript recognizes it you are going to miss cases or recognize cases where you shouldn't. And even if you can roll your own regex, why? There are simpler built-in ways to do it.
However, it turns out that
Number()
(andisNaN()
) does the right thing for every case whereparseFloat()
returns a number when it shouldn't, and vice versa. So to find out if a string is really exactly and only a number, call both functions and see if they both return true: