// val需要校验的数字,num需要保留的小数位constsetdecimals=(val, num)=>{
val = val.replace(/[^\d.-]/g,"");//清除"数字"和"."和-以外的字符
val = val.replace(/^\./g,"");//验证第一个字符是数字或者负号而不是.
val = val.replace(/\.{2,}/g,".");//只保留第一个.清除多余的let isF =false;if(val.substring(0,1)==='-'){// 是负数,做个标记
isF =true;}// 把所有负号先删除
val = val.replace(/-/g,"");
val = val
.replace(".","$#$").replace(/\./g,"").replace("$#$",".");switch(num){case1:
val = val.replace(/^(\\-)*(\d+)\.(\d).*$/,"$1$2.$3");//只能输入一个小数break;case2:
val = val.replace(/^(\\-)*(\d+)\.(\d\d).*$/,"$1$2.$3");//只能输入两个小数if(isF){// 删除后面其他负号
val ='-'+ val;}break;case3:
val = val.replace(/^(\\-)*(\d+)\.(\d\d\d).*$/,"$1$2.$3");//只能输入三个小数break;case4:
val = val.replace(/^(\\-)*(\d+)\.(\d\d\d\d).*$/,"$1$2.$3");//只能输入四个小数break;default:
val = val.replace(/^(\\-)*(\d+)\.(\d\d).*$/,"$1$2.$3");//只能输入两个小数break;}return val;};
下面是正数的校验方法
constsetdecimal=(val, num)=>{
val = val.replace(/[^\d.]/g,"");//清除"数字"和"."以外的字符
val = val.replace(/^\./g,"");//验证第一个字符是数字而不是
val = val.replace(/\.{2,}/g,".");//只保留第一个. 清除多余的
val = val
.replace(".","$#$").replace(/\./g,"").replace("$#$",".");switch(num){case1:
val = val.replace(/^(\\-)*(\d+)\.(\d).*$/,"$1$2.$3");//只能输入一个小数break;case2:
val = val.replace(/^(\\-)*(\d+)\.(\d\d).*$/,"$1$2.$3");//只能输入两个小数break;case3:
val = val.replace(/^(\\-)*(\d+)\.(\d\d\d).*$/,"$1$2.$3");//只能输入三个小数break;case4:
val = val.replace(/^(\\-)*(\d+)\.(\d\d\d\d).*$/,"$1$2.$3");//只能输入四个小数break;default:
val = val.replace(/^(\\-)*(\d+)\.(\d\d).*$/,"$1$2.$3");//只能输入两个小数break;}return val;};
matlab2016b可运行,输入城市位置,可以动态显示规划过程 % MTSPF_GA Fixed Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA)
% Finds a (near) optimal solution to a variation of the M-TSP by setting
% up a GA to search …
文章目录 课程简介Quiz 10: Tuples 单选题(1-11)编程题Exercise 10.2 课程简介
Python for Everybody 零基础程序设计(Python 入门) This course aims to teach everyone the basics of programming computers using Python. 本课…