This article mainly introduces how to obtain the decimal point of any decimal point in JavaScript. If you need a friend, refer to using Javascript to get the float type two digits after the decimal point, for example, 22.127456 to 22.13. What should I do?
1. This method is not recommended at all:
Function get () {var s = 22.127456 + ""; var str = s. substring (0, s. indexOf (".") + 3); alert (str );}
2. Use a regular expression to obtain:
Function get () {var a = "23.456322"; var aNew; var re =/([0-9] + \. [0-9] {2}) [0-9] */; aNew =. replace (re, "$1"); alert (aNew );}
3. relatively advanced applications:
Function get () {var num = 22.127456; alert (Math. round (num * 100)/100 );}
4. The simplest and most convenient:
Function get () {var num = new Number (13.37); num = num. toFixed (2); // 2 is the number of digits after the decimal point is obtained. It is automatically rounded to alert (num );}