Javascript decimal rounding implemented in multiple ways_Basic knowledge
- WBOYOriginal
- 2016-05-16 17:45:461087browse
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
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>
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