Home >Web Front-end >JS Tutorial >js retains two decimal places using toFixed_javascript skills

js retains two decimal places using toFixed_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:27:461336browse

Math.round(x*100)/100, of course what I want is Math.round(x*100), it doesn’t matter. But there is a problem with using Math.round(x*100) directly. Sometimes there is a small error, and many decimal places are displayed. For example, 0.9996*100 will become 99.96000000000001, which is not what I want.

Find the function of javascript, the number type has a toFixed() function, test:

Copy code The code is as follows :

alert((0.9996*100).toFixed(2));

Use Number.toFixed() to format numbers to display any decimal places!
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