JavaScript:parseFloat 不工作
我正在使用 jQuery 从网页中提取一些数据,它以数组形式返回,其中包含我试图添加的以下值(.5、.25、1.25、3.75)。这是我的代码片段:
var th = 0;
for (thisrow in objGrid) {
var hours = objGrid[thisrow]['hours'];
th = th+parseFloat(hours);
console.log(hours);
$("#msgbox").html("Showing records for week: " + thisDate + ". Total Hours for the week : " + th);
}
在 console.log 中,我得到了几个小时 - 0, 0 , 1, 3 ,总计 4。如果我不使用 parseFloat 我仍然得到相同的结果,(以为我会得到 NaN )。我做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您一定还有其他问题,因为您所描述的应该有效。例如(这是对代码的错误但直接的翻译):
无论如何,您应该对代码进行一些更改:
不要使用
for ( x in a )
进行迭代一个数组;使用数字 for 循环:for (var i=0,len=a.length;i
始终
var
你的变量。您正在使用并覆盖全局thisrow
变量。即使当您使用for ... in
时,也要像for (var x in o)
那样执行(除非您已在函数的前面声明了变量)。您应该使用
th += ...
而不是th = th + ...
,因为它更短且更干燥。您应该使用
*1
而不是<代码>parseFloat。它速度更快,打字更少,并且当您遇到混乱的字符串时也不会欺骗您。I think you must have some other problem, as what you are describing should work. For example (here's a bad-but-direct translation of your code):
A few things you should change about your code anyhow:
Don't use
for ( x in a )
to iterate over an array; use a numeric for loop:for (var i=0,len=a.length;i<len;++i)
Always
var
your variables. You're using and overwriting a globalthisrow
variable. Even when you usefor ... in
, do it like sofor (var x in o)
(unless you have declared your variable earlier in the function).You should use
th += ...
instead ofth = th + ...
, just because it's shorter and DRYer.You should use
*1
instead ofparseFloat
. It's faster, less typing, and doesn't lie to you when you have a janky string.应该可以解决问题。 ParseFloat() 获取字符编号的值。
should do the trick. ParseFloat() takes charaster number's value.
问题是:为什么你的小数值要四舍五入。在我自己摸索之后,我发现 for(... in ...) 构造本身正在将值向下舍入。如果直接访问数组元素,即 objGrid[0][i],则浮点数将保留其原始值,而不是四舍五入。因此,我的建议是使用标准的 for() 循环而不是 for(in)。
编辑:
对于那些不相信我的结果的人,请查看此代码片段:
这是OP问题的简化版本,但原理相同。查看警报语句的结果。它将是 0.5、.5,然后是 0 ... 所有这些都来自我引用变量的方式,该变量可能指向内存中的同一位置,但在后一种情况下,对值进行四舍五入(显然)。
我自己在最新版本的 Firefox 上测试了这一点。
The question is: why are your decimal values being rounded. After poking around myself, I found that the for(... in ...) construct itself is rounding the values down. If you access the array element directly, i.e. objGrid[0][i], then the floats will retain their original value, and not be rounded. Hence, my suggestions would be to use a standard for() loop instead of for(in).
EDIT:
For those disbelieving my results, check out this code snippet:
This is way simplified version of the OP's question, but same principle. Look at the results of the alert statements. It will be .5, .5, then 0 ... all from how I'm referencing the variable, which is presumably pointing to the same place in memory, but in the later case, rounding the value (apparently).
Tested this myself on the latest version of Firefox.
对此不确定,但尝试编写 var th = 0.0; - 也许,javascript 识别第一个整数并尝试将第二个操作数转换为整数。顺便问一下,你用什么浏览器?
Not sure about this, but try to write var th = 0.0; - maybe, javascript recognizes first integer number and tries to convert second operant to integer. By the way, what browser do you use?