


What are the regular ways to write whether it is an integer, decimal or real number?
这次给大家带来判断是否是整数,小数或实数正则有哪些写法,使用判断是否是整数,小数或实数正则的注意事项有哪些,下面就是实战案例,一起来看一下。
经常会遇到这样的情况,需要判断一个字符串是否是一个合法的数,包括整数,小数或者实数。
网上查到很多文章大多是判断这个字符串是否全为数字,比如下面这段来自StringUtils的代码,可以看到,13.2这样的数字实际上会返回false,可是,他的确是一个数字。
public static boolean isNumeric(String str) { if (str == null) { return false; } int sz = str.length(); for (int i = 0; i <p style="text-align: left;">当然,网上还能查到很多其他方式,诸如用<a href="http://www.php.cn/wiki/588.html" target="_blank">正则表达式</a>判断是否0-9,用字符ascii码判断是否是数字以及用Double.parseDouble()是否<a href="http://www.php.cn/php/php-tp-throw.html" target="_blank">抛出异常</a>来判断是否为数字。</p><p style="text-align: left;">事实上,除了最后一种方式能达到我们的要求,其他的都很难真正做到类似的判断。但是最后一种方式也很难区别出到底是正整数,负整数,正小数还是负小数,而且,捕获异常的方式实在是有些难看。</p><p style="text-align: left;">基于此原因,我自己写了一个工具类,专门用作数的检测,目前能够检测正整数,负整数,整数,正小数,负小数,小数以及实数,采用的仍然是正则表达式的方式,当然,如果有遗漏或者错误,欢迎联系我以便更正,同时也欢迎修改或使用这些代码以便符合你的应用场景。</p><p style="text-align: left;">可以简单讲下正则的思想以便修改,</p><p style="text-align: left;">1. 对于正整数而言,可以带+号,第一个数字不能为0</p><p style="text-align: left;">2. 对于负整数而言,必须带负号,第一个数字也不能为0</p><p style="text-align: left;">3. 对于整数而言,实际是由0,正整数和负整数组成的,所以偷个懒用前两个方法一起判断</p><p style="text-align: left;">4. 对于正小数而言,可以考带+号,并考虑两种情况,第一个数字为0和第一个数字不为0,第一个数字为0时,则小数点后面应该不为0,第一个数字不为0时,小数点后可以为任意数字</p><p style="text-align: left;">5. 对于负小数而言,必须带负号,其余都同上</p><p style="text-align: left;">6. 对于小数,可以带正负号,并且带小数点就行了,但是至少保证小数点有一边不为空,所以这里还是分左边不为空和右边不为空的情况</p><p style="text-align: left;">7. 实数比较简单,,要么是整数,要么是小数</p><pre class="brush:php;toolbar:false">package com.sap.cesp.creditinsight.web.app.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public class NumberValidationUtils { private static boolean isMatch(String regex, String orginal){ if (orginal == null || orginal.trim().equals("")) { return false; } Pattern pattern = Pattern.compile(regex); Matcher isNum = pattern.matcher(orginal); return isNum.matches(); } public static boolean isPositiveInteger(String orginal) { return isMatch("^\\+{0,1}[1-9]\\d*", orginal); } public static boolean isNegativeInteger(String orginal) { return isMatch("^-[1-9]\\d*", orginal); } public static boolean isWholeNumber(String orginal) { return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal); } public static boolean isPositiveDecimal(String orginal){ return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal); } public static boolean isNegativeDecimal(String orginal){ return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal); } public static boolean isDecimal(String orginal){ return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal); } public static boolean isRealNumber(String orginal){ return isWholeNumber(orginal) || isDecimal(orginal); } }
测试用例如下:
package com.sap.cesp.creditinsight.web.app.util; import junit.framework.Assert; import org.junit.Test; public class NumberValidationUtilsTest { /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isPositiveInteger(java.lang.String)} */ //correct test case: 1, 87653521123567 //wrong test case: 0.1, 0, 0123, -1, -0.1, ab @Test public void testIsPositiveInteger() { Assert.assertTrue(NumberValidationUtils.isPositiveInteger("1")); Assert.assertTrue(NumberValidationUtils.isPositiveInteger("+12")); Assert.assertTrue(NumberValidationUtils.isPositiveInteger("87653521123567")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0.1")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0123")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-1")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-0.1")); Assert.assertFalse(NumberValidationUtils.isPositiveInteger("ab")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isNegativeInteger(java.lang.String)} */ //correct test case: -1, -87653521123567 //wrong test case: 0.1, 0, 0123, 1, -0.1, -ab @Test public void testIsNegativeInteger() { Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-1")); Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-87653521123567")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0.1")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0123")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("1")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("-0.1")); Assert.assertFalse(NumberValidationUtils.isNegativeInteger("ab")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isWholeNumber(java.lang.String)}. */ //correct test case: -1, 0, 1, 8673434231, -282464334 //wrong test case: 0.1, 0123, -0.1, ab @Test public void testIsWholeNumber() { Assert.assertTrue(NumberValidationUtils.isWholeNumber("-1")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("0")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("1")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("+12")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("8673434231")); Assert.assertTrue(NumberValidationUtils.isWholeNumber("-282464334")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("0123")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("0.1")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("-0.1")); Assert.assertFalse(NumberValidationUtils.isWholeNumber("ab")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isPositiveDecimal(java.lang.String)} */ //correct test case: 0.1, 0.132213, 1.0 //wrong test case: 1, 0.0, 0123, -1, -0.1 @Test public void testIsPositiveDecimal() { Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("0.1")); Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("0.132213")); Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("30.00")); Assert.assertTrue(NumberValidationUtils.isDecimal("0.")); Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("+12.0")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0123")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("1")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0.0")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("ab")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-1")); Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-0.1")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isNegativeDecimal(java.lang.String)} */ //correct test case: -0.132213, -1.0 //wrong test case: 1, 0, 0123, -1, 0.1 @Test public void testIsNegativeDecimal() { Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-0.132213")); Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-1.0")); Assert.assertTrue(NumberValidationUtils.isDecimal("-0.")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("1")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0123")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.0")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("ab")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("-1")); Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.1")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isDecimal(java.lang.String)}. */ //correct test case: 0.1, 0.00, -0.132213 //wrong test case: 1, 0, 0123, -1, 0., ba @Test public void testIsDecimal() { Assert.assertTrue(NumberValidationUtils.isDecimal("0.1")); Assert.assertTrue(NumberValidationUtils.isDecimal("0.00")); Assert.assertTrue(NumberValidationUtils.isDecimal("+0.0")); Assert.assertTrue(NumberValidationUtils.isDecimal("-0.132213")); Assert.assertTrue(NumberValidationUtils.isDecimal("0.")); Assert.assertFalse(NumberValidationUtils.isDecimal("1")); Assert.assertFalse(NumberValidationUtils.isDecimal("0123")); Assert.assertFalse(NumberValidationUtils.isDecimal("0")); Assert.assertFalse(NumberValidationUtils.isDecimal("ab")); Assert.assertFalse(NumberValidationUtils.isDecimal("-1")); } /** * Test method for {@link com.sap.cesp.creditinsight.web.app.util.NumberValidationUtils#isRealNumber(java.lang.String)}. */ //correct test case: 0.032213, -0.234, 0.0, 1, -1, 0 //wrong test case: 00.13, ab, +0.14 @Test public void testIsRealNumber() { Assert.assertTrue(NumberValidationUtils.isRealNumber("0.032213")); Assert.assertTrue(NumberValidationUtils.isRealNumber("-0.234")); Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0")); Assert.assertTrue(NumberValidationUtils.isRealNumber("1")); Assert.assertTrue(NumberValidationUtils.isRealNumber("+0.14")); Assert.assertTrue(NumberValidationUtils.isRealNumber("-1")); Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0")); Assert.assertFalse(NumberValidationUtils.isRealNumber("00.13")); Assert.assertFalse(NumberValidationUtils.isRealNumber("ab")); } }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of What are the regular ways to write whether it is an integer, decimal or real number?. For more information, please follow other related articles on the PHP Chinese website!

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools
