Home >Web Front-end >HTML Tutorial >JS的乘积与运算精度_html/css_WEB-ITnose

JS的乘积与运算精度_html/css_WEB-ITnose

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-21 09:33:331580browse

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><script language="javascript">alert(3*2.7);</script></body></html>



为什么会弹出8.100000000001?
如何避免这种情况?

如果是3*8这样的整数运算,如何输出结果为24.00这样的格式?


回复讨论(解决方案)

numObj.toFixed(2)

js里的数字全部是浮点数,而浮点数的计算误差是不可避免的

非常感谢
把自己的运算结果加上.toFixed(2)就好了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn