Home > Java > JavaBase > body text

How to determine whether a string is a decimal in java

王林
Release: 2019-11-26 17:42:58
Original
4643 people have browsed it

How to determine whether a string is a decimal in java

函数介绍:

matches() 方法用于检测字符串是否匹配给定的正则表达式。

语法:

public boolean matches(String regex)
Copy after login

返回值:

在字符串匹配给定的正则表达式时,返回 true。

StringUtils.isBlank(String str)判断某字符串是否为空或长度为0或由空白符(whitespace) 构成。

免费相关学习视频推荐:java学习视频

示例如下:

 /**
     * 判断是否是整数或者是小数 
     * @param str
     * @return true:是,false不是
     */
    private boolean validateNumber(String str) {
        if(StringUtils.isBlank(str)) {
            return false;
        }
        // 说明一下的是该正则只能识别4位小数;如果不限制小数位数的话,写成[+-]?[0-9]+(\\.[0-9]+)?就可以了
        return str.matches("[+-]?[0-9]+(\\.[0-9]{1,4})?");    
    }
Copy after login

推荐相关文章教程:java零基础入门

The above is the detailed content of How to determine whether a string is a decimal in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!