This time I will bring you JS to retain one digit and then remove non-digits, and JS to retain one digit and then remove non-digits.What are the precautions?. Here is a practical case. Let’s take a look. one time.
//去除非数字 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; }
Supplement:
Let’s take a look at js processing numbers to retain 2 decimal places. It is not enough to force 2 decimal places. Add .00
1, keep two decimal places //Function: round the floating point number, take 2 digits after the decimal point
2, //keep 2 digits For decimals, such as: 2, 00 will be added after 2. That is 2.00
Test
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Vue environment passes parameters and packages different domain name code analysis
$http service Post method passes json parameters Detailed case explanation
The above is the detailed content of JS retains one digit and removes non-digits. For more information, please follow other related articles on the PHP Chinese website!