I know that examples such as 1.005*100 / 100
produce a slightly inaccurate result (100.49999999999999) in JavaScript, and this is intrinsic to floating point design.
(我知道例如1.005*100 / 100
100/100之类的示例在JavaScript中产生的结果有点不准确(100.49999999999999),这是浮点设计所固有的。)
My question is to see whether I can produce a reliable decimal round routine without the use of string manipulation.
(我的问题是看是否可以在不使用字符串操作的情况下生成可靠的十进制舍入例程。)
Where x
is an integer, will x / 100
(int/int=float) ever produce a visual inaccuracy?
(如果x
是整数, x / 100
(int / int = float)会不会产生视觉误差?)
(ie ..000001
or ..99999
) An example proving this would be preferable.((即..000001
或..99999
)证明这一点的示例将是更可取的。)
If the answer is yes then the only way to reliably move the decimal point towards the left in JavaScript is with string manipulation (ie MDN's Math.round10
), and to take the modest performance hit involved.
(如果答案是肯定的,那么可靠地将小数点向JavaScript中的左侧移动的唯一方法是使用字符串操作(即MDN的Math.round10
),并避免影响性能。)
ask by Bryan Field translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…