how to check if number is fraction or not in js?
- User
how to check if number is fraction or not in js?
Assistant
Here's how you can check if a number is a fraction in JavaScript:
function isFraction(num) { // Check if the number is not an integer return Number.isFinite(num) && !Number.isInteger(num); } // Examples console.log(isFraction(3.14)); // true console.log(isFraction(5.0)); // false (whole number) console.log(isFraction(10)); // false (integer) console.log(isFraction(1.5)); // trueAlternative approaches:
- Using modulo operator:
function isFraction(num) { return num % 1 !== 0; }- More comprehensive check:
function isFraction(num) { return Number.isFinite(num) && Math.floor(num) !== num; }Key points:
Number.isInteger()checks if the number is a whole number
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.