Home > Web Front-end > JS Tutorial > Javascript decimal rounding implemented in multiple ways_Basic knowledge

Javascript decimal rounding implemented in multiple ways_Basic knowledge

WBOY
Release: 2016-05-16 17:45:46
Original
1086 people have browsed it

Use Javascript to get the two decimal places after the float type. For example, 22.127456 is changed to 22.13. How to do it?
1. The stupidest way

Copy the code The code is as follows:

function get()
{
var s = 22.127456 "";
var str = s.substring(0,s.indexOf(".") 3);
alert(str ; >
The code is as follows:

3. He is smarter
.....




Copy code


The code is as follows:

<script> <strong>var num=22.127456; </strong> alert( Math.round(num*100)/100); <br></script>

Copy code


The code is as follows:

<script> <strong>var num=22.127456; </strong>alert( num.toFixed(2)); <br></script>
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template