在javascript中对字符串使用除法运算符(/) [英] using division operator (/) on strings in javascript

查看:527
本文介绍了在javascript中对字符串使用除法运算符(/)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到在javascript中所有101/100,101/ 100,101 /100和101/100导致1.01(在Chrome,FF和IE11上检查)。但是我找不到关于这种行为的文档。

I realized that in javascript all 101/100, "101"/100, 101/"100" and "101"/"100" result in 1.01 (checked on Chrome, FF and IE11). But I cannot find a piece of documentation regarding this behaviour.

因此我的问题是,如果(跨浏览器)安全使用此功能,如果它是这样做的好习惯(或者更确切地说,如果变量可以是字符串,则在分割前使用parseInt)?

Therefore my question is if it is (cross-browser) safe to use this feature, and if it is a good practice to do so (or rather to use parseInt before division if the variable can be a string)?

推荐答案


因此我的问题是,如果(跨浏览器)安全使用此功能...

Therefore my question is if it is (cross-browser) safe to use this feature...

这取决于你对安全的定义。使用除法运算符,是的,它是指定的行为:每个操作数被转换(隐式强制)为一个数字,然后进行数字除法。

It depends on your definition of "safe." With the division operator, yes, it's specified behavior: Each operand is converted (implicitly coerced) to a number, and then numeric division is done.

但要注意将此概括为太过分。您可以使用 / * - 但它会在 + 上咬你,因为如果 + 的任一操作数是一个字符串, + 执行字符串连接,而不是添加。

But beware of generalizing this too far. You'll be okay with /, *, and - but it will bite you on +, because if either operand to + is a string, + does string concatenation, not addition.

根据您的观点,它可能是安全的另一种方式是隐式强制:它使用浏览器的JavaScript引擎规则将字符串转换为数字。一些较旧的浏览器超出了规范(过去允许它们)并将 0 开头的数字视为 octal (基数为8)而不是小数。当然,输入0123作为数字的最终用户可能意味着数字123,而不是数字83(八进制中 123 = 83 十进制)。不再允许JavaScript引擎这样做,但是一些较旧的引擎会这样做。

Another way that it may or may not be "safe" depending on your point of view is the implicit coercion: It uses the browser's JavaScript engine's rules for converting strings to numbers. Some older browsers went beyond the specification (which they were allowed to in the past) and treated numbers starting with a 0 as octal (base 8) rather than decimal. Naturally, end users who type in, say, "0123" as a number probably mean the number 123, not the number 83 (123 in octal = 83 decimal). JavaScript engines are no longer allowed to do that, but some older ones do.

通常,最好明确强制或转换这些操作数。您这样做的选择:

In general, it's probably best to explicitly coerce or convert those operands. Your choices for doing so:


  • 一元 + 运算符: value = + value 将使用JavaScript引擎的标准规则将字符串强制转换为数字。字符串中的任何非数字(科学计数法的 e 除外)都会产生结果 NaN 。此外, + 0 ,这可能不直观。

  • The unary + operator: value = +value will coerce the string to a number using the JavaScript engine's standard rules for that. Any non-digits in the string (other than the e for scientific notation) make the result NaN. Also, +"" is 0, which may not be intuitive.

Number 函数: value = Number(value)。与 + 相同。

parseInt 函数,通常带基数(数字基数): value = parseInt(value,10)。这里的缺点是 parseInt 转换它在字符串开头找到的任何数字,但忽略字符串后面的非数字,所以 parseInt(100asdf,10) 100 ,而不是 NaN 。顾名思义, parseInt 只解析整数。

The parseInt function, usually with a radix (number base): value = parseInt(value, 10). The downside here is that parseInt converts any number it finds at the beginning of the string but ignores non-digits later in the string, so parseInt("100asdf", 10) is 100, not NaN. As the name implies, parseInt parses only a whole number.

parseFloat function: value = parseFloat(value)。允许小数值,并始终以十进制(从不八进制或十六进制)工作。同样的事情 parseInt 是否在字符串末尾有垃圾, parseFloat(123.34alksdjf) 123.34

The parseFloat function: value = parseFloat(value). Allows fractional values, and always works in decimal (never octal or hex). Does the same thing parseInt does with garbage at the end of the string, parseFloat("123.34alksdjf") is 123.34.

因此,选择适合您使用的工具案件。 : - )

So, pick your tool to suit your use case. :-)

这篇关于在javascript中对字符串使用除法运算符(/)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆