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.
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);//不是小数
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);//含小数点
[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!

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

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

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

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

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

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

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

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


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
