JS控制浮点数输入正则

需求:

1、过滤以0开头
2、去除所有非数字和小数点的字符
3、去掉重复的小数点

JS实时控制浮点数输入

1
2
3
4
5
amountWatcher (val, oldVal) {
console.log(val)
this.order.amount = val.replace(/^[0]+/, '')
this.order.amount = this.order.amount.replace(/[^\d.]|\.(?=[^.]*\.)/g, '')
}

Java校验字符串实现

1
2
3
4
5
6
7
String[] source = { "1,736, 233.00", " 236. 233.73", "36,233.235", "34.00" };
String[] result = new String[source.length];
for (int i = 0; i < source.length; i++) {
result[i] = source[i].replaceAll("([^\\d\\.]|\\.(?=[^\\.]*\\.))", "");
}
System.out.println(Arrays.toString(result));
// [1736233.00, 236233.73, 36233.235, 34.00]

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :