Explain Codes LogoExplain Codes Logo

Check if a number has a decimal place/is a whole number

javascript
prompt-engineering
functions
performance
Alex KataevbyAlex Kataev·Sep 24, 2024
TLDR

To determine if a number is a whole number or has a decimal place, use the modulo (%) operator. If the operation returns a non-zero remainder, it's a decimal. Here's the code:

const isWhole = num => num % 1 === 0; console.log(isWhole(10)); // Can't argue with 10, that's whole. true console.log(isWhole(10.5)); // Definitely having a decimal crisis. false

Deep dive into identifying decimal and whole numbers

Differentiating between decimal and whole numbers can be crucial for mathematical operations, validation, or formatting. Let's explore various methods and edge cases.

Modulo operator versatility

The num % 1 pattern determines whether a number is a whole number and extracts the decimal part. It even treats fixed decimal strings like 12.0 as integers!

const getDecimalPart = num => num % 1; console.log(getDecimalPart(10.5)); // Well, it's the best 0.5 you can get

Embracing Number.isInteger()

You can use Number.isInteger() for an all-encompassing check. However, its lack of support in IE11 might turn off legacy browsers.

console.log(Number.isInteger(10)); // Ten? That's numberwang! true console.log(Number.isInteger(NaN)); // Not-a-number? More like not-an-integer. false console.log(Number.isInteger(Infinity)); // To infinity... but not beyond. false

Tread safely with Number.isSafeInteger()

If the number could be astronomically big, use Number.isSafeInteger(). It checks if values are within IEEE-754 double-precision limits.

console.log(Number.isSafeInteger(10**16)); // It's big, but it's safe. true console.log(Number.isSafeInteger(10**16+1)); // A single step beyond. false

String-based approach

Stringified numbers? Check for the absence of a decimal point. Remember, "100.00" isn't a whole number.

const isWholeFromStr = strNum => strNum.indexOf(".") === -1; console.log(isWholeFromStr("100.00")); // It's not about the zeroes, it's about the dot. false

Math.floor(num) == num to the rescue

One more way to check for whole numbers – Math.floor(num) == num. Old but gold.

console.log(Math.floor(10) === 10); // Ten stands strong. true console.log(Math.floor(10.5) === 10.5); // Half point down. false

Choose your method based on performance, readability, and compatibility, dear code ninja. Remember, num % 1 could be faster, while Number.isInteger(num) can offer precision.

JavaScript has special numeric values and edge cases begging your attention. Let's examine:

Dealing with NaN and Infinity

Handling NaN and Infinity requires special checks. Your trusty modulo operator might let you down here.

const isActualNumber = num => !isNaN(num) && isFinite(num) && num % 1 === 0; console.log(isActualNumber(NaN)); // NaN calling itself a number? Bad joke. false console.log(isActualNumber(Infinity)); // Even an infinite number has its limits. false

Jumbo numbers

__ For numbers beyond Number.MAX_SAFE_INTEGER, JavaScript might start acting funny. Brace yourself and use BigInt or libraries like big.js for arbitrary precision.