Navigate back to the homepage

How to check value is decimal or not in javascript?

Infinitbility
JavaScript
November 3rd, 2021 · 1 min read
How to check value is decimal or not in javascript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to check value is decimal or not in javascript, here we will you below three ways to check decimal numbers.

  1. Javascript Number.isInteger() Method
  2. Javascript Math.floor() Method
  3. Javascript indexOf() Method

Let’s start today’s tutorial How to check value is decimal or not in javascript?

Javascript Number.isInteger() Method

Javascript provide Number.isInteger() to identify given data is whole number or decimal number.

1!Number.isInteger(12.00); // false
2!Number.isInteger(12); // false
3!Number.isInteger(-12); // false
4!Number.isInteger(-12.003); // true
5!Number.isInteger(25.00000000000001); // true
6!Number.isInteger(25.00000000000000001); // false

Number.isInteger() is part of the ES6 standard, and you are using an older version, you have should move to the second solution.

Note: Number.isInteger() to find the decimal place, you have ”.” After and before 13 digits, the value must be greater than 0.

output

isNan, Javascript, Example

Javascript Math.floor() Method

For check decimal number, we will use Javascript Math.floor() Method, it will same work like Number.isInteger() method.

1function isDecimal(n) {
2 var result = n - Math.floor(n) !== 0;
3
4 if (result) return true;
5 else return false;
6}
7
8console.log(isDecimal(12.00)); // false
9console.log(isDecimal(12)); // false
10console.log(isDecimal(-12)); // false
11console.log(isDecimal(-12.002)); // true
12console.log(isDecimal(25.000000000001)); // true
13console.log(isDecimal(25.000000000000001)); // false

Note: isDecimal(n) to find the decimal place, you have ”.” After and before 13 digits, the value must be greater than 0.

output

isDecimal, Math.floor(), Javascript, Example

Javascript indexOf() Method

If you want to get isDecimal to provide true when you pass 0.00, and 12.00 then you will help indexOf() Method, it only checks ”.” in provided value, and if it is available it will return true.

1!("12.00".indexOf(".") == -1); // true
2!("12".indexOf(".") == -1); // false
3!("-12".indexOf(".") == -1); // false
4!("-12.003".indexOf(".") == -1); // true
5!("25.00000000000001".indexOf(".") == -1); // true
6!("25.00000000000000001".indexOf(".") == -1); // true

Note: to use indexOf() method you have to first convert value number to string.

output

indexOf, Javascript, Example

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to check value is number or not in javascript?

How to check value is number or not in javascript?

Example of check value is number or not in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 2nd, 2021 · 1 min read
Guidebook The Javascript Monorepo

Guidebook The Javascript Monorepo

Example of Javascript Monorepo

November 1st, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]