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
Release: 2016-05-16 17:27:46
Original
1338 people have browsed it

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!
Related labels:
source:php.cn
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