- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
Example 1 knockout extenders 页面的描述了一种舍入用户输入并确保它只是数字的方法。
它工作得很好,但是通过查看源代码他们做了一件我不明白的奇怪的事情,那就是在第 8 行他们这样做:
parseFloat(+newValue)
newValue
是一个字符串。
当我最初问这个问题时,我不知道 +
做了什么 - 更进一步 poking和一个 link to a different MDN page从我得到的一个最初的答案表明它是一个等价于 number(str)
的一元运算符并且 +str
和 parseFloat(str )
(以字母字符结尾的字符串的处理和十六进制的解释似乎是头条新闻)。
我仍然不明白为什么在这种情况下 +
需要包装在 parseFloat
中,尽管我开始认为这可能是一个错字......
最佳答案
为 parseFloat
引用 MDN 文档:
<罢工>使用 [unary plus operator][2] 您可以确定 `parseFloat` 对 `Number` 进行操作,这仅在您想要对结果更严格但仍想使用 `parseFloat` 时才有用
parseFloat
parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.
parseFloat('0.32abcd') // -> 0.32
parseFloat(+'0.32abcd') // -> NaN
**更新:**
在深入研究文档并运行一些测试后,似乎没有理由使用 parseFloat
除了解析可能包含带有非数字尾迹的数字的字符串外,eq:
parseFloat('31.5 miles') // -> 31.5
parseFloat('12.75em') // -> 12.75
对于您的字符串包含数字 +
的任何其他情况是一种最快且首选的方式(引用 MDN docs for unary plus operator ):
unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number.
参见 parseFloat versus unary test case它有多快。
上一个链接断开所以 here is the new test这表明一元的速度有多快。
关于javascript - + 运算符与 parseFloat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394852/
Number.parseFloat() 和 parseFloat() 有什么区别?一个比另一个好吗? 最佳答案 它们的功能完全相同。他们不仅行为相同。它们是完全相同的函数对象。 为了扩展这一点,Num
我很抱歉,但我需要帮助:( 我尝试将字符串转换为十进制并且它有效,但我有一些问题: number = document.getElementById("totalcost").innerHTM
如何在名为 parseFloat 的 Angular 工厂函数内访问 parseFloat? angular .module('myApp') .factory('mathServic
我从客户的内部 API 获取了一个庞大的数据集。它将包含一堆价格的数字数据,例如:31.23 美元。他把它们作为 {"spend":"21.23"} 给了我,这很好,但我担心在 1000 多个项目并在
这个问题已经有答案了: How to round to at most 2 decimal places, if necessary (91 个回答) 已关闭 7 年前。 我已经阅读了一些示例,了解如
我有一个使用 .toLocaleString() 格式化的输入字段。 然后我需要对该值使用 parseFloat() 将其转换为数字。 要在美国语言环境中执行此操作,我只需在执行 parseFloat
我有以下情况,我正在使用jquery,我需要总结表单上的一些字段。我在小计字段和总计字段中发现了 NaN 错误。我已尽一切可能避免此类错误,我只需要此字段上的 SUM 。我的表单中的所有内容都工作正常
我有一个类别页面,其中显示不同的产品。在每个产品中,我都有一个部分显示原价、保存价格 和新价格。只是为了说明原价的结构: Was £
我有一个文本框,我需要将一个 float 值传递给 api。 现在当我在文本框中输入值“18.575”或“18.09483”时一切正常。 但如果值为“18.000”,则无法正常工作。我知道 18.00
好的,所以我的代码工作正常,但是当为 #chance 输入一个小数,即 60.1、60.2、60.3 等时,它会搞砸利润和支付。 例如:60%为机会输入,1为赌注。它返回 1.65 的工资和 0.65
我正在使用 jQuery 从网页中提取一些数据,它作为一个数组返回,其中包含我尝试添加的以下值 (.5、.25、1.25、3.75)。这是我的代码片段: var th = 0; for (th
我正在学习 Javascript。 在 javascript 中有 parseFloat 和 parseInt 函数是什么意思,事实上,Javascript 不区分 float 和整数——只有数字数据
我正在尝试解析数组中的值,然后将它们添加到图表中。问题是我在输出中得到了一些意想不到的数字。我遍历并填充我的数组。在按钮上单击我循环遍历并解析数据并将其放入另一个数组以传递给图表系列。这在理论上可行,
这个问题在这里已经有了答案: Is floating point math broken? (31 个答案) 关闭 9 年前。 我正在尝试将“9999999999999099”转换为 javascr
我正在从事一个需要“提示框”计算系统的项目。正如您在代码片段中看到的那样,它没有像我预期的那样工作。我该如何解决这个问题? $("select[name='tip']").on('change',fu
我需要验证文本框条目。它应该只允许整数,因为如果用户在绑定(bind)期间在此字段中输入字符,entityframework 将失败。 这里是问题: 当用户在此字段中输入 10-d 时,parseFl
在我的代码中,我有这个: function addNumbers() { var collect = parseFloat(document.getElementBy
我确信这与我对行为的理解有关,而不是 parseFloat 方面的任何不一致操作,但请耐心等待... 如果我输入格式为“IDCODE: (3.5)” 其中IDCODE可能是几个不同的预定义字符串,并且
我在 HTML 表单中有一些输入,使用 javascript 在更改时自动填充输入 问题是下面的代码显示了小数,更长的小数像这样 42880.34623465464356435634 代码是: the
我在转换输入数字时遇到问题,当插入 1,050000 时,它给我的结果不是 1.050000,而是 1.000000。 parseFloat("1,050000").toFixed(6).toStri
我是一名优秀的程序员,十分优秀!