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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-21 09:33:33
Original
1579 people have browsed it

<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>
Copy after login



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

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


回复讨论(解决方案)

numObj.toFixed(2)

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

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

Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template