From f0c49618962ca83d7943f94d658f014b213f30c6 Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Sun, 1 May 2022 12:46:00 +0300 Subject: [PATCH] Add hasDecimals --- snippets/hasDecimals.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/hasDecimals.md diff --git a/snippets/hasDecimals.md b/snippets/hasDecimals.md new file mode 100644 index 000000000..ce56701d5 --- /dev/null +++ b/snippets/hasDecimals.md @@ -0,0 +1,20 @@ +--- +title: Number has decimal digits +tags: math +expertise: beginner +author: chalarangelo +firstSeen: 2022-05-13T05:00:00-04:00 +--- + +Checks if a number has any decimals digits + +- Use the modulo (`%`) operator to check if the number is divisible by `1` and return the result. + +```js +const hasDecimals = num => num % 1 !== 0; +``` + +```js +hasDecimals(1); // false +hasDecimals(1.001); // true +```