What are the methods of javascript to detect decimals
This article is about what javascript has to do with decimal detection. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Using indexOf (), the syntax "String (num) .indexOf (". ")", if the return value is greater than "- 1", it is a decimal.
Function isDot (num) {if (String (num) .indexOf (".") >-1) {console.log ("is a decimal");} else {console.log ("not a decimal");} isDot (121.121); / / isDot (454654) with decimal point; / / without decimal point
2. Using regular expressions, the syntax "var rep=/ [\.] /; rep.test (num)".
Function isDot (num) {var rep=/ [\.] /; if (rep.test (num)) {console.log ("is a decimal");} else {console.log ("not a decimal");}} isDot (121.121); / / decimal isDot (454.654); / / decimal isDot (454654); / / not decimal thanks for reading! This is the end of this article on "what are the decimal detection methods of javascript?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!