I'm trying to write a helper function that will cast a String
ing from an <input type="text" />
to a Number
.
As I wasn't sure whether to use parseFloat(str)
or Number(str)
I doublechecked how they handle potentially problematic arguments.
See:
console.log(Number(null)); // 0
console.log(parseFloat(null)); // NaN
console.log(parseInt(null)); // NaN
console.log(isNaN(null)); // false
I'm trying to write a helper function that will cast a String
ing from an <input type="text" />
to a Number
.
As I wasn't sure whether to use parseFloat(str)
or Number(str)
I doublechecked how they handle potentially problematic arguments.
See:
console.log(Number(null)); // 0
console.log(parseFloat(null)); // NaN
console.log(parseInt(null)); // NaN
console.log(isNaN(null)); // false
Both parseFloat
and parseInt
return NaN
, whereas Number
returns 0
. Number
seems more coherent here with isNaN(null)
.
Why is that?
Share Improve this question edited Apr 1, 2019 at 15:57 connexo asked Apr 1, 2019 at 15:48 connexoconnexo 56.9k15 gold badges111 silver badges146 bronze badges 4- 5 its part of the standard. read the docs – Daniel A. White Commented Apr 1, 2019 at 15:50
-
for validation use regexp
/^-?\d*\.?\d*$/.test(stringWithNumber)
– Kamil Kiełczewski Commented Apr 1, 2019 at 15:52 - This question has been answered on this link – Aderbal Farias Commented Apr 1, 2019 at 15:53
- Number() !== parseFloat(). Removed explanation, better in answers below. – Someone Commented Apr 1, 2019 at 15:55
4 Answers
Reset to default 3Number
constructor tries to coerce the argument to number. So empty string '', false
, null
and all falsy values bee 0.
Similarly, Number(true)
will return 1. Number('some string')
will be NaN
as 'some string' cannot be converted to a number.
Note that as pointed out in the ments, Number(undefined)
is NaN and not 0 in arithmetic operations. (Read here https://codeburst.io/understanding-null-undefined-and-nan-b603cb74b44c)
Read more here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
parseInt/Float
convert their argument to a string, read it char by char from the left and try to make a number from what they've found. SinceString(null)
is"null"
and a decimal number cannot start with"n"
,parseInt(null)
will beNaN
. If you provide a different base, wheren
,u
andl
are digits, the result will be different:
console.log(parseInt(null, 32))
Number
converts its argument as a whole into a number.Number(null)
returns+0
because the ECMA mittee wants it to: http://www.ecma-international/ecma-262/7.0/#sec-tonumber . This is probably for historical reasons.global
isNaN
(not to confuse withNumber.isNaN
) appliesNumber
to its argument and returnstrue
if the result isNaN
. SinceNumber(null)
is+0
,isNaN(null)
isfalse
.
Hope this sheds some light...
NaN stands for "Not a Number". ParseFloat and ParseInt return real numbers and integers, so this is like an error returned by the function. Number(), on the other hand, represents the object's value. For instance, Number(false) will output 0.
The reason seems to be quite subtle with how parseInt
and parseFloat
work:
If the argument passed to parseInt
/parseFloat
is not a String
(which null isn't), then it calls toString(null)
which returns "[object Undefined]"
.
If the first character cannot be converted to a number, parseInt returns NaN. https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744835592a4596242.html
评论列表(0条)