1. The stupidest Way
function get () { var s = 22.127456 + ""; var str = s.substring (0,s.indexof (".") + 3); alert (str);}
2. Regular expression effect is good
<script type= "Text/javascript" >function() { var a = "23.456322"; var anew; var re =/([0-9]+.[ 0-9]{2}) [0-9]*/; = A.replace (Re, "$"); alert (anew);} </script>
3. He is more clever ...
<script>var num=22.127456; alert (math.round (num*100)/100);</script>
4.js Reserved 2 decimal places (mandatory)
For the number of decimal places greater than 2 bits, with the above function is not a problem, but if less than 2 bits, such as: Changetwodecimal (3.1), will return 3.1, if you must need 3.10 such a format, then you need the following function:
functionChangetwodecimal_f (x) {varf_x =parsefloat (x); if(IsNaN (f_x)) {alert (' Function:changetwodecimal->parameter error '); return false; } varf_x = Math.Round (x * 100)/100; varS_x =f_x.tostring (); varPos_decimal = S_x.indexof ('. ')); if(Pos_decimal < 0) {Pos_decimal=s_x.length; S_x+ = '. '; } while(s_x.length <= Pos_decimal + 2) {s_x+ = ' 0 '; } returns_x;}
JS method to get two decimal places