Skip to main content

How to Get the Decimal Part of a Number in JavaScript

A common task in programming is to isolate the decimal (or fractional) part of a number. For example, you might need to separate 0.75 from 12.75. JavaScript provides several ways to do this, but the best method depends on your goal: do you need a precise mathematical value, or a string representation?

This guide will teach you the two primary methods for this task. We'll cover the string manipulation approach, which is the most reliable for avoiding floating-point precision issues, and the mathematical approach using the modulo operator, which is more concise but has important limitations.

The Core Problem: Mathematical vs. String Representation

It's important to decide what you need as your output:

  • A String: If you need the digits 345 from the number 12.345, you are performing a string operation.
  • A Number: If you need the mathematical value 0.345 from the number 12.345, you are performing a mathematical operation.

The string-based approach is generally safer and more predictable due to the nature of floating-point arithmetic in JavaScript.

This is the most robust and reliable method because it avoids the quirks of floating-point math. It treats the number as a sequence of characters.

The logic:

  1. Check if the number is an integer. If so, it has no decimal part, so return 0.
  2. Convert the number to a string using .toString().
  3. Split the string at the decimal point (.) into an array of two parts.
  4. The second element of the array (at index 1) is the string of digits you want.
  5. Convert this string back to a number.

Solution:

function getDecimalPart(num) {
if (Number.isInteger(num)) {
return 0;
}

const decimalStr = num.toString().split('.')[1];
return Number(decimalStr);
}

// Example Usage:
console.log(getDecimalPart(12.345)); // Output: 345
console.log(getDecimalPart(-1.23)); // Output: 23
console.log(getDecimalPart(10)); // Output: 0
note

This method is predictable and always gives you the exact digits that were after the decimal point.

The Mathematical Method: The Modulo (%) Operator

For a purely mathematical approach, the modulo (or "remainder") operator (%) offers a very concise solution. The expression num % 1 calculates the remainder when num is divided by 1, which is the fractional part.

Solution:

function getDecimalPart(num) {
// This works for positive numbers
return num % 1;
}

// Example Usage:
console.log(getDecimalPart(3.14)); // Output: 0.14000000000000012 (potential precision issue)
console.log(getDecimalPart(5)); // Output: 0

How to handle Negative Numbers

The modulo operator behaves differently with negative numbers. To get a consistent positive result, you can use Math.abs().

function getDecimalPart(num) {
return Math.abs(num % 1);
}

console.log(getDecimalPart(-3.14)); // Output: 0.14000000000000012

The Pitfall of Floating-Point Math

The mathematical methods (% 1 or num - Math.trunc(num)) are subject to the inherent limitations of binary floating-point representation. Computers cannot perfectly represent all decimal fractions, which can lead to tiny precision errors.

Example: these small errors can cause bugs if you are performing strict equality checks.

console.log(0.1 + 0.2); // Output: 0.30000000000000004

const result = 1.23 % 1;
console.log(result); // Output: 0.22999999999999998
note

Why the string method is safer?

  • By converting the number to a string first, you are working with the exact decimal representation that was provided, not the computer's underlying binary approximation.
  • This makes the toString().split('.') method the most reliable choice for most use cases.

Conclusion

For getting the decimal part of a number, the best method depends on your specific needs, but the string-based approach is generally the safest.

  • The toString().split('.') method is the recommended best practice. It is the most reliable and predictable way to get the decimal digits, as it avoids the pitfalls of floating-point arithmetic.
  • The mathematical methods (like the modulo operator % 1) are more concise but can introduce small precision errors. They should be used with caution, especially if you need to perform exact comparisons.