Home Web Front-end JS Tutorial What are the regular ways to write whether it is an integer, decimal or real number?

What are the regular ways to write whether it is an integer, decimal or real number?

Mar 30, 2018 pm 02:45 PM
decimal

这次给大家带来判断是否是整数,小数或实数正则有哪些写法,使用判断是否是整数,小数或实数正则的注意事项有哪些,下面就是实战案例,一起来看一下。

经常会遇到这样的情况,需要判断一个字符串是否是一个合法的数,包括整数,小数或者实数。

网上查到很多文章大多是判断这个字符串是否全为数字,比如下面这段来自StringUtils的代码,可以看到,13.2这样的数字实际上会返回false,可是,他的确是一个数字。

  public static boolean isNumeric(String str) { 
    if (str == null) { 
      return false; 
    } 
    int sz = str.length(); 
    for (int i = 0; i < sz; i++) { 
      if (Character.isDigit(str.charAt(i)) == false) { 
        return false; 
      } 
    } 
    return true; 
  }
Copy after login

当然,网上还能查到很多其他方式,诸如用正则表达式判断是否0-9,用字符ascii码判断是否是数字以及用Double.parseDouble()是否抛出异常来判断是否为数字。

事实上,除了最后一种方式能达到我们的要求,其他的都很难真正做到类似的判断。但是最后一种方式也很难区别出到底是正整数,负整数,正小数还是负小数,而且,捕获异常的方式实在是有些难看。

基于此原因,我自己写了一个工具类,专门用作数的检测,目前能够检测正整数,负整数,整数,正小数,负小数,小数以及实数,采用的仍然是正则表达式的方式,当然,如果有遗漏或者错误,欢迎联系我以便更正,同时也欢迎修改或使用这些代码以便符合你的应用场景。

可以简单讲下正则的思想以便修改,

1. 对于正整数而言,可以带+号,第一个数字不能为0

2. 对于负整数而言,必须带负号,第一个数字也不能为0

3. 对于整数而言,实际是由0,正整数和负整数组成的,所以偷个懒用前两个方法一起判断

4. 对于正小数而言,可以考带+号,并考虑两种情况,第一个数字为0和第一个数字不为0,第一个数字为0时,则小数点后面应该不为0,第一个数字不为0时,小数点后可以为任意数字

5. 对于负小数而言,必须带负号,其余都同上

6. 对于小数,可以带正负号,并且带小数点就行了,但是至少保证小数点有一边不为空,所以这里还是分左边不为空和右边不为空的情况

7. 实数比较简单,,要么是整数,要么是小数

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); 
  } 
 
}
Copy after login

测试用例如下:

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")); 
     
  } 
 
}
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

JS的正则如何校验非零的正整数

JS里最基础的正则表达式使用详解

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles