Languages
[Edit]
EN

JavaScript - has number decimal part

8 points
Created by:
telsa
352

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

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