In JavaScript, when converting from a float to a string, how can I get just 2 digits after the decimal point? For example, 0.34 instead of 0.3445434. There are functions to round numbers. For example: var x = 5.0364342423; print(x.toFixed(2)); will print 5.04. EDIT: Fiddle Be careful when using toFixed() : First, rounding the number is done using the binary representation of the numbe
在JavaScript中,当从float转换为字符串时,如何在小数点后得到2位数字? 例如,0.34而不是0.3445434。 有函数来整数。 例如: var x = 5.0364342423; print(x.toFixed(2)); 将打印5.04。 编辑:小提琴 使用toFixed()时要小心: 首先,使用数字的二进制表示将数字四舍五入,这可能会导致意外的行为。 例如 (0.595).toFixed(2) === '0.59' 而不是'0.6' 。 其次, toFixed()有一个IE错误。 在IE中(至少
Why does 49.90 % 0.10 in JavaScript return 0.09999999999999581 ? I expected it to be 0. Because JavaScript uses floating point math which always leads to rounding errors. If you need an exact result with two decimal places, multiply your numbers with 100 before the operation and then divide again afterwards: var result = ( 4990 % 10 ) / 100; Round if necessary. Javascript's Number is
为什么JavaScript中的49.90 % 0.10返回0.09999999999999581 ? 我预计它是0。 由于JavaScript使用总是导致舍入错误的浮点数学运算。 如果您需要两位小数的精确结果,请在操作前将您的数字乘以100 ,然后再进行分割: var result = ( 4990 % 10 ) / 100; 如有必要,轮次 Javascript的编号使用“IEEE双精度”来存储值。 他们无法准确存储所有的十进制数字。 由于将十进制数转换为二进制时的舍入误差,结果不为零。 49.90
Possible Duplicate: Is JavaScript's Math broken? Okay, I have this script: var x = new Date; setInterval(function() { $("#s").text((new Date - x) / 60000 + "Minutes Wasted"); }, 30000); It works perfectly fine. Except it sometimes gives me an answer like 4.000016666666666 . How can I round that? If I have to rewrite the script, that is okay. Thanks! You can use Math.floor() fu
可能重复: JavaScript的数学是否被破坏? 好的,我有这个脚本: var x = new Date; setInterval(function() { $("#s").text((new Date - x) / 60000 + "Minutes Wasted"); }, 30000); 它工作得很好。 除了它有时会给我一个像4.000016666666666的答案。 我怎么能这样做? 如果我必须重写脚本,那没关系。 谢谢! 您可以使用Math.floor()函数“向下舍入” $("#s").text(Math.floor((new Date - x) / 60000 + "Min
This question already has an answer here: Is floating point math broken? 23 answers Floating point numbers can't handle decimals correctly in all cases. Check out http://en.wikipedia.org/wiki/Floating-point_number#Accuracy_problems http://www.mredkj.com/javascript/nfbasic2.html You should be aware that all information in computers is in binary and the expansions of fractions in di
这个问题在这里已经有了答案: 浮点数学是否被破坏? 23个答案 在所有情况下,浮点数都不能正确处理小数。 查看 http://en.wikipedia.org/wiki/Floating-point_number#Accuracy_problems http://www.mredkj.com/javascript/nfbasic2.html 你应该知道,计算机中的所有信息都是二进制的,不同基地的分数的扩展是不同的。 例如,基数10中的1/3 = .33333333333333333333333333,而基数3中的1/3等于.1,基数2中的1/3等
Possible Duplicate: Is JavaScript's Math broken? Why does JS screw up this simple math? document.write(.1 + .2) // 0.3000000000000004 document.write(.3 + .6) // 0.8999999999999999 The first example is greater than the correct result, while the second is less. ???!! How do you fix this? Do you have to always convert decimals into integers before performing operations? Do I only hav
可能重复: JavaScript的数学是否被破坏? 为什么JS搞砸了这个简单的数学? document.write(.1 + .2) // 0.3000000000000004 document.write(.3 + .6) // 0.8999999999999999 第一个例子比正确的结果大,而第二个例子少。 ??? !! 你如何解决这个问题? 在执行操作之前,您是否必须始终将小数转换为整数? 我只需要担心添加(*和/在我的测试中似乎没有相同的问题)? 我在很多地方寻找答案。 有些教程(如购物车表
This question already has an answer here: Is floating point math broken? 23 answers Use toFixed to convert it to a string with some decimal places shaved off, and then convert it back to a number. +(0.1 + 0.2).toFixed(12) // 0.3 It looks like IE's toFixed has some weird behavior, so if you need to support IE something like this might be better: Math.round((0.1 + 0.2) * 1e12) / 1e12 fu
这个问题在这里已经有了答案: 浮点数学是否被破坏? 23个答案 使用toFixed将其转换为带有一些小数位的字符串,然后将其转换回数字。 +(0.1 + 0.2).toFixed(12) // 0.3 它看起来像IE的toFixed有一些奇怪的行为,所以如果你需要支持IE这样的事情可能会更好: Math.round((0.1 + 0.2) * 1e12) / 1e12 function add(){ var first=parseFloat($("#first").val()); var second=parseFloat($("#second").val()); $(
Possible Duplicate: Is JavaScript's Math broken? I'm calculating the sum of several float values using javascript and... I've noticed a strange thing never seen before. Executing this code: parseFloat('2.3') + parseFloat('2.4') I obtain 4.699999999999999 So... what sould I do to obtain a correct value? (supposed that this is incorrect...) 一旦你阅读了每个计算机科学家应该知
可能重复: JavaScript的数学是否被破坏? 我正在计算使用javascript和几个浮点值的总和...我注意到一个从未见过的奇怪的事情。 执行此代码: parseFloat('2.3') + parseFloat('2.4') 我获得了4.699999999999999 所以...我该怎么做才能获得正确的价值? (假设这是不正确的......) 一旦你阅读了每个计算机科学家应该知道的关于浮点算术的知识,你可以使用.toFixed()函数: var result = parseFloat('2.3') + parseFlo
I have a overlay DIV with absolute position width height and width 100%. But when I click on the overlay DIV, click is happening on underlying elements of overlay DIV. Overlay DIV style is like below .overlayDIV{ absolute : absolute; height : 100%; width : 100%; z-index : 1234; } How can I prevent that...? This problem exists only in Windows mobile You can st
我有一个绝对位置宽度高度和宽度100%的覆盖DIV。 但是当我点击叠加DIV时,点击正在叠加DIV的底层元素上发生。 叠加DIV样式如下所示 .overlayDIV{ absolute : absolute; height : 100%; width : 100%; z-index : 1234; } 我怎样才能防止...? 此问题仅在Windows Mobile中存在 您可以停止事件的传播: JavaScript的: document.getElementById('divId').onclick = function (event) {
I have a menu that consists of a submenu and then a slideshow (made with bxslider plugin) that displays when hovering over any particular submenu li. The slideshow fades in and fades out, but when I hover over a new submenu li after a slideshow is visible, the transitions overlap and prevents me from hovering over the new slideshow. Can I cancel an existing slideshow css transition when hoverin
我有一个菜单,其中包含一个子菜单,然后是一个幻灯片(用bxslider插件制作),当鼠标悬停在任何特定的子菜单上时显示。 幻灯片淡入淡出,但当幻灯片显示后,当我将鼠标悬停在新的子菜单li上时,转场重叠并阻止我将鼠标悬停在新的幻灯片上。 悬停在新的子菜单li上时,我可以取消现有的幻灯片CSS转换吗? HTML: <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" type="text/css" h
I was trying to make fade out transition with css3 but found out that display is not working with transitions. i have a group object <div id=groupID> with <h2> title and <div class='group'> and i have onclick event bind to <h2> that should make group class to dissapear. i've tried to overcome display problem with {opacity: 0; height: 0; overflow: hidden;} {
我试图使用css3淡出转换,但发现显示不适用于转换。 我有一个<h2>标题和<div class='group'>组对象<div id=groupID> ,并且我有onclick事件绑定到<h2>应该使组类不起作用。 我试图克服显示问题{opacity: 0; height: 0; overflow: hidden;} {opacity: 0; height: 0; overflow: hidden;} {opacity: 0; height: 0; overflow: hidden;}这样做效果很好,因为它与{display:none}效果相同,但是