Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,181 questions

56,073 answers

573 users

How to check whether a floating-point variable is an integer (has no fractional part) in JavaScript

3 Answers

0 votes
let d = 24278.0;

if (d == Math.floor(d)) {
    console.log("The double variable contains an integer");
}
else {
    console.log("The double variable contains a floating point");
}




/*
run:

The double variable contains an integer

*/

 



answered Mar 7, 2024 by avibootz
0 votes
function main() {
    // Example input values (JavaScript numbers behave like doubles)
    const a = 42.0;                 // integer-like
    const b = 42.00000000001;       // floating-point due to tiny fraction
    const c = 89.0000000000001;     // integer-like
    const d = -7.0;                 // negative integer-like
    const e = 3.14;                 // floating-point
 
    console.log(Number.isInteger(a)); 
    console.log(Number.isInteger(b)); 
    console.log(Number.isInteger(c)); 
    console.log(Number.isInteger(d)); 
    console.log(Number.isInteger(e)); 
}
 
main(); 
  
  
/*
run:
  
true
false
false
true
false
  
*/
 

 



answered Mar 7, 2024 by avibootz
edited Sep 10 by avibootz
0 votes
// -----------------------------------------------------------------------------
// Determines whether a number contains an integer (no fractional part).
// Uses Number.isInteger for clarity and a small tolerance for precision issues.
// -----------------------------------------------------------------------------
function isInteger(value) {
    const epsilon = 1e-12;

    // Number.isInteger handles clean integer cases efficiently.
    // For floating‑point edge cases, compare against a rounded version.
    return Number.isInteger(value) || Math.abs(value - Math.round(value)) < epsilon;
}

// -----------------------------------------------------------------------------
// Prints a descriptive message for the given number.
// -----------------------------------------------------------------------------
function printCheck(value) {
    console.log(`Checking value: ${value}`);

    if (isInteger(value)) {
        console.log("Result: The value contains an integer.\n");
    } else {
        console.log("Result: The value contains a floating‑point number.\n");
    }
}

// -----------------------------------------------------------------------------
// Entry point demonstrating several example values.
// -----------------------------------------------------------------------------
function main() {
    // Example input values (JavaScript numbers behave like doubles)
    const a = 42.0;                 // integer-like
    const b = 42.00000000001;       // floating-point due to tiny fraction
    const c = 89.0000000000001;     // integer-like
    const d = -7.0;                 // negative integer-like
    const e = 3.14;                 // floating-point

    // Run checks
    printCheck(a);
    printCheck(b);
    printCheck(c);
    printCheck(d);
    printCheck(e);
}

main();



/*
run:

Checking value: 42
Result: The value contains an integer.

Checking value: 42.00000000001
Result: The value contains a floating‑point number.

Checking value: 89.0000000000001
Result: The value contains an integer.

Checking value: -7
Result: The value contains an integer.

Checking value: 3.14
Result: The value contains a floating‑point number.

*/

 



answered Sep 10 by avibootz

Related questions

...