Use Javascript to get the first two digits of the float decimal point. For example, 22.127456 is used to get 22.13. How can this problem be solved?
1. the most stupid way
Copy codeThe Code is as follows: function get ()
{
Var s = 22.127456 + "";
Var str = s. substring (0, s. indexOf (".") + 3 );
Alert (str );
}
2. The regular expression works well. Copy codeThe Code is as follows: <script type = "text/javascript">
Onload = function (){
Var a = "23.456322 ";
Var aNew;
Var re =/([0-9] + \. [0-9] {2}) [0-9] */;
ANew = a. replace (re, "$1 ");
Alert (aNew );
}
</Script>
3. He is smart......Copy codeThe Code is as follows: <script>
Var num = 22.127456;
Alert (Math. round (num * 100)/100 );
</Script>
4. Friends who will use fresh things... But IE5.5 + is required.Copy codeThe Code is as follows: <script>
Var num = 22.127456;
Alert (num. toFixed (2 ));
</Script>