這篇文章主要介紹JS中如何移除非數(shù)字最多保留一位小數(shù),文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
js中移除非數(shù)字最多保留一位小數(shù)的實現(xiàn)代碼如下所示:
//去除非數(shù)字 var clearNoNum = function (item) { if (item!=null && item!=undefined) { //先把非數(shù)字的都替換掉,除了數(shù)字和. item = item.replace(/[^\d.]/g, ""); //必須保證第一個為數(shù)字而不是. item = item.replace(/^\./g, ""); //保證只有出現(xiàn)一個.而沒有多個. item = item.replace(/\.{2,}/g, ""); //保證.只出現(xiàn)一次,而不能出現(xiàn)兩次以上 item = item.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //最多保留小數(shù)點后一位 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處理數(shù)字保留2位小數(shù),強制保留2位小數(shù)不夠補上.00
1、保留兩位小數(shù) //功能:將浮點數(shù)四舍五入,取小數(shù)點后2位
2、//制保留2位小數(shù),如: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"> //保留兩位小數(shù) //功能:將浮點數(shù)四舍五入,取小數(shù)點后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return; } f = Math.round(x*100)/100; return f; } //制保留2位小數(shù),如: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位小數(shù):" + toDecimal(3.14159267)+"<br/>"); document.write("3.14159267強制保留2位小數(shù):" + toDecimal2(3.14159267)+"<br/>"); document.write("3.14159267保留2位小數(shù):" + toDecimal(3.14559267)+"<br/>"); document.write("3.14159267強制保留2位小數(shù):" + toDecimal2(3.15159267)+"<br/>"); document.write("3.14159267保留2位小數(shù):" + fomatFloat(3.14559267, 2)+"<br/>"); document.write("3.14159267保留1位小數(shù):" + fomatFloat(3.15159267, 1)+"<br/>"); document.write("五舍六入 <br/>") document.write("1000.003保留2位小數(shù):" + 1000.003.toFixed(2)+"<br/>"); document.write("1000.08保留1位小數(shù):" + 1000.08.toFixed(1)+"<br/>"); document.write("1000.04保留1位小數(shù):" + 1000.04.toFixed(1)+"<br/>"); document.write("1000.05保留1位小數(shù):" + 1000.05.toFixed(1)+"<br/>"); document.write("科學計數(shù) <br/>") document.write(3.1415+"科學技術(shù)后:"+3.1415.toExponential(2)+"<br/>"); document.write(3.1455+"科學技術(shù)后:"+3.1455.toExponential(2)+"<br/>"); document.write(3.1445+"科學技術(shù)后:"+3.1445.toExponential(2)+"<br/>"); document.write(3.1465+"科學技術(shù)后:"+3.1465.toExponential(2)+"<br/>"); document.write(3.1665+"科學技術(shù)后:"+3.1665.toExponential(1)+"<br/>"); document.write("精確到n位,不含n位 <br/>") document.write("3.1415精確到小數(shù)點第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1455精確到小數(shù)點第3位" + 3.1465.toPrecision(3)+"<br/>"); document.write("3.1445精確到小數(shù)點第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1465精確到小數(shù)點第2位" + 3.1455.toPrecision(2)+"<br/>"); document.write("3.166592679287精確到小數(shù)點第5位" + 3.141592679287.toPrecision(5)+"<br/>"); </script> <body> <input type="text" id="Score" /> </body> </html>
以上是“JS中如何移除非數(shù)字最多保留一位小數(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:JS中如何移除非數(shù)字最多保留一位小數(shù)-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://jinyejixie.com/article12/dsehgc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、微信公眾號、關(guān)鍵詞優(yōu)化、網(wǎng)站營銷、自適應網(wǎng)站、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容