search
HomeWeb Front-endFront-end Q&AHow to determine whether it is a decimal in ES6

How to determine whether it is a decimal in ES6

Oct 11, 2022 pm 05:09 PM
javascriptes6es6 string

Two judgment methods: 1. Use the test() function with the regular expression "/[.]/" to check whether the specified value contains a decimal point, the syntax is "/[.]/.test(specified value) ”, if included, it is a decimal, otherwise it is not. 2. Use the indexOf() function to check whether the specified value contains a decimal point. The syntax is "String(specified value).indexOf(".")". If the return value is greater than "-1", it is a decimal, and vice versa.

How to determine whether it is a decimal in ES6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Decimals are "." with a decimal point. In JavaScript, you can determine whether a number is a decimal by judging whether it contains "." with a decimal point. .

Method 1: Use the test() function with regular expressions to check

The test() method is used to detect whether a string matches a certain pattern. , searches the string for text that matches the regular expression. If a match is found, it returns true; otherwise, it returns false.

RegExpObject.test(string)

Regular expression for checking decimals: /[.]/

Example:

function isDot(num) {
	var rep=/[.]/;
	if(rep.test(num)){
		console.log(num+" 是小数");
	}
	else{
		console.log(num+" 不是小数");
	}
}
isDot(121.121);//是小数
isDot(454.654);//是小数
isDot(454654);//不是小数

How to determine whether it is a decimal in ES6

Method 2: Use the indexOf() function to check

The indexOf() method can return the position where a specified string value first appears in the string.

string.indexOf(searchvalue,start)
Parameters Description
searchvalue Required . Specifies the string value to be retrieved.
start Optional integer parameter. Specifies the position in the string to begin searching. Its legal values ​​are 0 to string Object.length - 1. If this parameter is omitted, the search will start from the first character of the string.

Return value: Find the first occurrence of the specified string. If no matching string is found, -1 will be returned.

Just use indexOf() to check the position where the character "." first appears in the string. If the return value is equal to -1, it is a decimal, greater than -1 is not a decimal.

Example:

function isDot(num) {
	if(String(num).indexOf(".")>-1){
		console.log(num+" 是小数");
	}
	else{
		console.log(num+" 不是小数");
	}
}
isDot(121.121);//含有小数点
isDot(454654);//不含小数点
isDot(45465.4);//含小数点

How to determine whether it is a decimal in ES6

[Recommended learning: javascript advanced tutorial]

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

Statement
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
What are the limitations of React?What are the limitations of React?May 02, 2025 am 12:26 AM

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

React's Learning Curve: Challenges for New DevelopersReact's Learning Curve: Challenges for New DevelopersMay 02, 2025 am 12:24 AM

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate

Generating Stable and Unique Keys for Dynamic Lists in ReactGenerating Stable and Unique Keys for Dynamic Lists in ReactMay 02, 2025 am 12:22 AM

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

JavaScript Fatigue: Staying Current with React and Its ToolsJavaScript Fatigue: Staying Current with React and Its ToolsMay 02, 2025 am 12:19 AM

JavaScriptfatigueinReactismanageablewithstrategieslikejust-in-timelearningandcuratedinformationsources.1)Learnwhatyouneedwhenyouneedit,focusingonprojectrelevance.2)FollowkeyblogsliketheofficialReactblogandengagewithcommunitieslikeReactifluxonDiscordt

Testing Components That Use the useState() HookTesting Components That Use the useState() HookMay 02, 2025 am 12:13 AM

TotestReactcomponentsusingtheuseStatehook,useJestandReactTestingLibrarytosimulateinteractionsandverifystatechangesintheUI.1)Renderthecomponentandcheckinitialstate.2)Simulateuserinteractionslikeclicksorformsubmissions.3)Verifytheupdatedstatereflectsin

Keys in React: A Deep Dive into Performance Optimization TechniquesKeys in React: A Deep Dive into Performance Optimization TechniquesMay 01, 2025 am 12:25 AM

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

What are keys in React?What are keys in React?May 01, 2025 am 12:25 AM

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

The Importance of Unique Keys in React: Avoiding Common PitfallsThe Importance of Unique Keys in React: Avoiding Common PitfallsMay 01, 2025 am 12:19 AM

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor