Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript determine whether a specified value is positive or not

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces javascript how to judge whether the specified value is a positive number of related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you read this javascript how to determine whether the specified value is a positive number of articles will have a harvest, let's take a look.

Judgment method: 1. Use "var reg = / ^\ d + (? =\. {0Magne1}\ dexpressions $| $) /" to define the regular expression object reg;2, use the "reg.test (specify value)" statement to determine whether the specified value is a positive number, if it returns true, it is a positive number, otherwise it is not a positive number.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript determines whether the specified value is positive or not

In javascript, regular expressions can be used to determine whether a specified value is positive.

Implementation code:

Function validate (num) {if (num! = 0) {var reg = / ^\ d + (? =\. {0jue 1}\ daut $| $) /; if (reg.test (num)) {console.log (num+ "is positive");} else {console.log (num+ "not positive") }} else {console.log ("0 is neither positive nor negative");}} validate (- 1); validate (- 2); validate (0); validate (1); validate (1.5); validate (2.365)

Description:

The test () method is used to detect whether a string matches a pattern. Returns true if there is a matching value in the string, false otherwise.

Syntax:

RegExpObject.test (string)

Extended knowledge: expressions for checking numbers

Number: ^ [0-9] * $

N-digit number: ^\ d {n} $

At least n-digit number: ^\ d {n,} $

The number of mmurn digits: ^\ d {mrecom n} $

Zero and non-zero numbers: ^ (0 | [1-9] [0-9] *) $

A number with up to two decimal places at the beginning of a non-zero: ^ ([1-9] [0-9] *) + (. [0-9] {1J2})? $

Positive or negative numbers with 1-2 decimal places: ^ (\ -)?\ d + (\. D {1jin2})? $

Positive, negative, and decimal: ^ (\-|\ +)?\ d + (\.\ d +)? $

A positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})? $

A positive real number with 1 to 3 decimal places: ^ [0-9] + (. [0-9] {1jue 3})? $

Non-zero positive integers: ^ [1-9]\ During $or ^ ([1-9] [0-9] *) {1 ~ (st) 3} $or ^\ +? [1-9] [0-9] * $

Non-zero negative integers: ^\-[1-9] [] 0-9 "* $or ^-[1-9]\ dflowers $

Non-negative integer: ^\ dBy $or ^ [1-9]\ d* | 0 $

Non-positive integer: ^-[1-9]\ d* | 0$ or ^ ((-\ d+) | (0 +)) $

Non-negative floating point number: ^\ d + (\.\ d +)? $or ^ [1-9]\ d *.\ d * | 0.\ d * [1-9]\ d * | 0?\ .0 + | 0 $

Non-positive floating point number: ^ ((-\ d + (\.\ d +)?) | (0 + (\ .0 +)?) $or ^ (- ([1-9]\ d *\.\ d * | 0\.\ d * [1-9]\ d *)) | 0?\ .0 + | 0 $

Positive floating point number: ^ [1-9]\ d *\.\ d * | 0\.\ d * [1-9]\ dfloat $or ^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *) $)

Negative floating point numbers: ^-([1-9]\ d *\.\ d * | 0\.\ d * [1-9]\ d *) $or ^ (0-9) +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $

Floating point number: ^ (-?\ d +) (\.\ d +)? $or ^ -? ([1-9]\ d *.\ d * | 0.\ d * [1-9]\ d * | 0?\ .0 + | 0) $

This is the end of the article on "how javascript determines whether the specified value is positive or not". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to judge whether the specified value is positive or not". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report