Java开发 之小数点保留后两位、千分位符、实数正则校验

import java.util.regex.Pattern;

public class DefaultTest {

    private static final Pattern PATTERN = Pattern.compile("^[-]?(\\d{1,3}(,\\d{3})*(\\.\\d{1,2})?)");

    public static void main(String[] args) {
        System.out.println("5 === " + PATTERN.matcher("5").matches());
        System.out.println("5. === " + PATTERN.matcher("5.").matches());
        System.out.println("5.1 === " + PATTERN.matcher("5.1").matches());
        System.out.println("5.12 === " + PATTERN.matcher("5.12").matches());
        System.out.println("5.123 === " + PATTERN.matcher("5.123").matches());
        System.out.println("0.12 === " + PATTERN.matcher("0.12").matches());
        System.out.println("1,006.12 === " + PATTERN.matcher("1,006.12").matches());
        System.out.println("1,006.123 === " + PATTERN.matcher("1,006.123").matches());
        System.out.println("1,006. === " + PATTERN.matcher("1,006.").matches());
        System.out.println("1,006.1 === " + PATTERN.matcher("1,006.1").matches());
        System.out.println("51,006.1 === " + PATTERN.matcher("51,006.1").matches());
        System.out.println("3,541,006.1 === " + PATTERN.matcher("3,541,006.1").matches());
        System.out.println("100446.12 === " + PATTERN.matcher("100446.12").matches());
        System.out.println("10,06.1 === " + PATTERN.matcher("10,06.1").matches());
        System.out.println("51,006.133 === " + PATTERN.matcher("51,006.133").matches());
        System.out.println("3,541,006 === " + PATTERN.matcher("3,541,006").matches());
        System.out.println("1,001 === " + PATTERN.matcher("1,001").matches());
        System.out.println("-1,001 === " + PATTERN.matcher("-1,001").matches());
        System.out.println("+1,001 === " + PATTERN.matcher("+1,001").matches());
        System.out.println("3333,891,001 === " + PATTERN.matcher("3333,891,001").matches());
    }
}
5 === true
5. === false
5.1 === true
5.12 === true
5.123 === false
0.12 === true
1,006.12 === true
1,006.123 === false
1,006. === false
1,006.1 === true
51,006.1 === true
3,541,006.1 === true
100446.12 === false
10,06.1 === false
51,006.133 === false
3,541,006 === true
1,001 === true
-1,001 === true
+1,001 === false
3333,891,001 === false
上一篇:Promise与Rxjs的区别


下一篇:[RxJS] SwitchAll() - switch between two observables display always latest value