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+"科學技術后:"+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精確到小數(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ù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- js保留兩位小數(shù)方法總結
- JS實現(xiàn)保留n位小數(shù)的四舍五入問題示例
- js實現(xiàn)四舍五入完全保留兩位小數(shù)的方法
- JavaScript如何實現(xiàn)對數(shù)字保留兩位小數(shù)一位自動補零
- javascript實現(xiàn)保留兩位小數(shù)的多種方法
- 實現(xiàn)js保留小數(shù)點后N位的代碼
- Js保留小數(shù)點的4種效果實現(xiàn)代碼分享
- js保留小數(shù)點后幾位的寫法
- js浮點數(shù)保留兩位小數(shù)點示例代碼(四舍五入)
- JS保留兩位小數(shù) 四舍五入函數(shù)的小例子
- JS格式化數(shù)字金額用逗號隔開保留兩位小數(shù)
- JS格式化數(shù)字保留兩位小數(shù)點示例代碼
相關文章
JS實現(xiàn)的自定義顯示加載等待圖片插件(loading.gif)
這篇文章主要介紹了JS實現(xiàn)的自定義顯示加載等待圖片插件,涉及javascript針對圖片的動態(tài)加載實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06js模仿php中strtotime()與date()函數(shù)實現(xiàn)方法
這篇文章主要介紹了js模仿php中strtotime()與date()函數(shù)實現(xiàn)方法,涉及javascript時間操作的相關技巧,非常具有實用價值,需要的朋友可以參考下2015-08-08延時重復執(zhí)行函數(shù) lLoopRun.js
延時重復執(zhí)行函數(shù) lLoopRun.js...2007-05-05