js中移除非數字最多保留一位小數的實現代碼如下所示:
//去除非數字 var clearNoNum = function (item) { if (item!=null && item!=undefined) { //先把非數字的都替換掉,除了數字和. item = item.replace(/[^\d.]/g, ""); //必須保證第一個為數字而不是. item = item.replace(/^\./g, ""); //保證只有出現一個.而沒有多個. item = item.replace(/\.{2,}/g, ""); //保證.只出現一次,而不能出現兩次以上 item = item.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //最多保留小數點后一位 var arr = item.split("."); if (arr.length > 1) item = arr[0] + '.' + (arr[1].length > 1 ? arr[1].substr(0, 1) : arr[1]); } return item; }
補充:
下面看下js處理數字保留2位小數,強制保留2位小數不夠補上.00
1、保留兩位小數 //功能:將浮點數四舍五入,取小數點后2位
2、//制保留2位小數,如:2,會在2后面補上00.即2.00
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Test</title> <script type="text/javascript" src="js/jq.js"></script> </head> <script type="text/javascript"> //保留兩位小數 //功能:將浮點數四舍五入,取小數點后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return; } f = Math.round(x*100)/100; return f; } //制保留2位小數,如:2,會在2后面補上00.即2.00 function toDecimal2(x) { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x*100)/100; var s = f.toString(); var rs = s.indexOf('.'); if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + 2) { s += '0'; } return s; } function fomatFloat(src,pos){ return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos); } document.write("四舍五入 <br/>") document.write("3.14159267保留2位小數:" + toDecimal(3.14159267)+"<br/>"); document.write("3.14159267強制保留2位小數:" + toDecimal2(3.14159267)+"<br/>"); document.write("3.14159267保留2位小數:" + toDecimal(3.14559267)+"<br/>"); document.write("3.14159267強制保留2位小數:" + toDecimal2(3.15159267)+"<br/>"); document.write("3.14159267保留2位小數:" + fomatFloat(3.14559267, 2)+"<br/>"); document.write("3.14159267保留1位小數:" + fomatFloat(3.15159267, 1)+"<br/>"); document.write("五舍六入 <br/>") document.write("1000.003保留2位小數:" + 1000.003.toFixed(2)+"<br/>"); document.write("1000.08保留1位小數:" + 1000.08.toFixed(1)+"<br/>"); document.write("1000.04保留1位小數:" + 1000.04.toFixed(1)+"<br/>"); document.write("1000.05保留1位小數:" + 1000.05.toFixed(1)+"<br/>"); document.write("科學計數 <br/>") document.write(3.1415+"科學技術后:"+3.1415.toExponential(2)+"<br/>"); document.write(3.1455+"科學技術后:"+3.1455.toExponential(2)+"<br/>"); document.write(3.1445+"科學技術后:"+3.1445.toExponential(2)+"<br/>"); document.write(3.1465+"科學技術后:"+3.1465.toExponential(2)+"<br/>"); document.write(3.1665+"科學技術后:"+3.1665.toExponential(1)+"<br/>"); document.write("精確到n位,不含n位 <br/>") document.write("3.1415精確到小數點第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1455精確到小數點第3位" + 3.1465.toPrecision(3)+"<br/>"); document.write("3.1445精確到小數點第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1465精確到小數點第2位" + 3.1455.toPrecision(2)+"<br/>"); document.write("3.166592679287精確到小數點第5位" + 3.141592679287.toPrecision(5)+"<br/>"); </script> <body> <input type="text" id="Score" /> </body> </html>
總結
以上所述是小編給大家介紹的JS中移除非數字最多保留一位小數,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com