JS中移除非數(shù)字最多保留一位小數(shù)
js中移除非數(shù)字最多保留一位小數(shù)的實(shí)現(xiàn)代碼如下所示:
//去除非數(shù)字 var clearNoNum = function (item) { if (item!=null && item!=undefined) { //先把非數(shù)字的都替換掉,除了數(shù)字和. item = item.replace(/[^\d.]/g, ""); //必須保證第一個(gè)為數(shù)字而不是. item = item.replace(/^\./g, ""); //保證只有出現(xiàn)一個(gè).而沒(méi)有多個(gè). item = item.replace(/\.{2,}/g, ""); //保證.只出現(xiàn)一次,而不能出現(xiàn)兩次以上 item = item.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //最多保留小數(shù)點(diǎn)后一位 var arr = item.split("."); if (arr.length > 1) item = arr[0] + '.' + (arr[1].length > 1 ? arr[1].substr(0, 1) : arr[1]); } return item; }
補(bǔ)充:
下面看下js處理數(shù)字保留2位小數(shù),強(qiáng)制保留2位小數(shù)不夠補(bǔ)上.00
1、保留兩位小數(shù) //功能:將浮點(diǎn)數(shù)四舍五入,取小數(shù)點(diǎn)后2位
2、//制保留2位小數(shù),如:2,會(huì)在2后面補(bǔ)上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ù) //功能:將浮點(diǎn)數(shù)四舍五入,取小數(shù)點(diǎn)后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return; } f = Math.round(x*100)/100; return f; } //制保留2位小數(shù),如:2,會(huì)在2后面補(bǔ)上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強(qiáng)制保留2位小數(shù):" + toDecimal2(3.14159267)+"<br/>"); document.write("3.14159267保留2位小數(shù):" + toDecimal(3.14559267)+"<br/>"); document.write("3.14159267強(qiáng)制保留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("科學(xué)計(jì)數(shù) <br/>") document.write(3.1415+"科學(xué)技術(shù)后:"+3.1415.toExponential(2)+"<br/>"); document.write(3.1455+"科學(xué)技術(shù)后:"+3.1455.toExponential(2)+"<br/>"); document.write(3.1445+"科學(xué)技術(shù)后:"+3.1445.toExponential(2)+"<br/>"); document.write(3.1465+"科學(xué)技術(shù)后:"+3.1465.toExponential(2)+"<br/>"); document.write(3.1665+"科學(xué)技術(shù)后:"+3.1665.toExponential(1)+"<br/>"); document.write("精確到n位,不含n位 <br/>") document.write("3.1415精確到小數(shù)點(diǎn)第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1455精確到小數(shù)點(diǎn)第3位" + 3.1465.toPrecision(3)+"<br/>"); document.write("3.1445精確到小數(shù)點(diǎn)第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1465精確到小數(shù)點(diǎn)第2位" + 3.1455.toPrecision(2)+"<br/>"); document.write("3.166592679287精確到小數(shù)點(diǎn)第5位" + 3.141592679287.toPrecision(5)+"<br/>"); </script> <body> <input type="text" id="Score" /> </body> </html>
總結(jié)
以上所述是小編給大家介紹的JS中移除非數(shù)字最多保留一位小數(shù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- js保留兩位小數(shù)方法總結(jié)
- JS實(shí)現(xiàn)保留n位小數(shù)的四舍五入問(wèn)題示例
- js實(shí)現(xiàn)四舍五入完全保留兩位小數(shù)的方法
- JavaScript如何實(shí)現(xiàn)對(duì)數(shù)字保留兩位小數(shù)一位自動(dòng)補(bǔ)零
- javascript實(shí)現(xiàn)保留兩位小數(shù)的多種方法
- 實(shí)現(xiàn)js保留小數(shù)點(diǎn)后N位的代碼
- Js保留小數(shù)點(diǎn)的4種效果實(shí)現(xiàn)代碼分享
- js保留小數(shù)點(diǎn)后幾位的寫(xiě)法
- js浮點(diǎn)數(shù)保留兩位小數(shù)點(diǎn)示例代碼(四舍五入)
- JS保留兩位小數(shù) 四舍五入函數(shù)的小例子
- JS格式化數(shù)字金額用逗號(hào)隔開(kāi)保留兩位小數(shù)
- JS格式化數(shù)字保留兩位小數(shù)點(diǎn)示例代碼
相關(guān)文章
JS實(shí)現(xiàn)的自定義顯示加載等待圖片插件(loading.gif)
這篇文章主要介紹了JS實(shí)現(xiàn)的自定義顯示加載等待圖片插件,涉及javascript針對(duì)圖片的動(dòng)態(tài)加載實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06js模仿php中strtotime()與date()函數(shù)實(shí)現(xiàn)方法
這篇文章主要介紹了js模仿php中strtotime()與date()函數(shù)實(shí)現(xiàn)方法,涉及javascript時(shí)間操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08延時(shí)重復(fù)執(zhí)行函數(shù) lLoopRun.js
延時(shí)重復(fù)執(zhí)行函數(shù) lLoopRun.js...2007-05-05js根據(jù)當(dāng)前日期獲取前一周或者后一周等日期
有的時(shí)候要獲取當(dāng)前日期,或者前一天、后一天的日期,下面這篇文章主要給大家介紹了關(guān)于js根據(jù)當(dāng)前日期獲取前一周或者后一周等日期的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04