Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of javascript to detect decimals

Shulou Source: shulou.com Published: 2022-06-03 14:55:24 11月14日 Update

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!

Tags: Decimal method detection content decimal point more article grammar good practical article regular look knowledge expression reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Huawei MariaDB Linux