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

2021/10/12 17:15:47

本文主要是介绍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


这篇关于Java开发 之小数点保留后两位、千分位符、实数正则校验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程