java parseint 负号_JavaScript-js parseInt和Number都不认识负号,即parseInt(“-280px”)不认识,返回NaN...

这篇博客深入探讨了Firefox浏览器中parseInt函数的源码实现,特别关注了其如何处理负数的情况。源码分析表明,parseInt确实能够识别并处理负数,这反驳了一些关于其不支持负数的误解。文章详细解释了源码中的关键步骤,包括负数判断、基数确定以及数字解析等过程,对于理解JavaScript内置函数的工作原理非常有帮助。
摘要由CSDN通过智能技术生成

看了下firefox的parseInt的源码,确实有负数判断啊

static bool

ParseIntStringHelper(JSContext *cx, const jschar *ws, const jschar *end, int maybeRadix,

bool stripPrefix, jsdouble *dp)

{

JS_ASSERT(maybeRadix == 0 || (2 <= maybeRadix && maybeRadix <= 36));

JS_ASSERT(ws <= end);

const jschar *s = js_SkipWhiteSpace(ws, end);

JS_ASSERT(ws <= s);

JS_ASSERT(s <= end);

/* 15.1.2.2 steps 3-4. 这里就是负数判断,何来的不认一说呢*/

bool negative = (s != end && s[0] == '-');

/* 15.1.2.2 step 5. */

if (s != end && (s[0] == '-' || s[0] == '+'))

s++;

/* 15.1.2.2 step 9. */

int radix = maybeRadix;

if (radix == 0) {

if (end - s >= 2 && s[0] == '0' && (s[1] != 'x' && s[1] != 'X')) {

/*

* Non-standard: ES5 requires that parseInt interpret leading-zero

* strings not starting with "0x" or "0X" as decimal (absent an

* explicitly specified non-zero radix), but we continue to

* interpret such strings as octal, as per ES3 and web practice.

*/

radix = 8;

} else {

radix = 10;

}

}

/* 15.1.2.2 step 10. */

if (stripPrefix) {

if (end - s >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {

s += 2;

radix = 16;

}

}

/* 15.1.2.2 steps 11-14. */

const jschar *actualEnd;

if (!GetPrefixInteger(cx, s, end, radix, &actualEnd, dp))

return false;

if (s == actualEnd)

*dp = js_NaN;

else if (negative)

*dp = -*dp;

return true;

}

补充下图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值