문제

Why:

parseInt(-0, 10) // 0
parseInt('-0', 10) // -0

Is there a sensible reason that parseInt wouldn't do -0 for both or is this just an oddity of javascript?

도움이 되었습니까?

해결책

parseInt() works on strings. Running parseInt() on a number causes it to be cast to a string.

(-0).toString() is 0.

다른 팁

The first parameter to the parseInt is converted to a String, as per ECMA 5.1 Spec for parseInt,

1. Let inputString be ToString(string).

And ToString, returns '0', even if the input is +0 or -0,

2. If m is +0 or −0, return the String "0".

That is why you are getting this behavior.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top