国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JS中移除非數字最多保留一位小數

2024-05-06 15:34:31
字體:
來源:轉載
供稿:網友

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>             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 黄陵县| 阜宁县| 洛隆县| 南昌县| 平原县| 玉田县| 望江县| 三门峡市| 青冈县| 开平市| 三穗县| 色达县| 潜山县| 建宁县| 新兴县| 噶尔县| 革吉县| 尉氏县| 高要市| 康保县| 嘉鱼县| 南雄市| 游戏| 射洪县| 赤壁市| 文登市| 门头沟区| 贵阳市| 吉林省| 崇仁县| 日喀则市| 马山县| 旺苍县| 黄梅县| 宁阳县| 建瓯市| 丹江口市| 清水县| 太仓市| 苗栗市| 杨浦区|