Languages
[Edit]
EN

JavaScript - number has decimal part

8 points
Created by:
telsa
502

In this short article, we would like to show how to check if number has decimal part using JavaScript.

Practical example:

// ONLINE-RUNNER:browser;

const hasDecimalPart = value => {
  	return isFinite(value) && (value % 1.0 !== 0);
};


// Usage example:

console.log(hasDecimalPart(  5     ));       //   false
console.log(hasDecimalPart(  3.14  ));       //   true
console.log(hasDecimalPart( -3.14  ));       //   true
console.log(hasDecimalPart(  0.123 ));       //   true
console.log(hasDecimalPart( -0.123 ));       //   true

console.log(hasDecimalPart( '+3.14'    ));   //   true
console.log(hasDecimalPart( '-3.14'    ));   //   true
console.log(hasDecimalPart(   NaN      ));   //   false
console.log(hasDecimalPart(  'foo'     ));   //   false
console.log(hasDecimalPart(  -Infinity ));   //   false
console.log(hasDecimalPart(  +Infinity ));   //   false

See also

  1. JavaScript - get decimal part of float number

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join