Retrieving the decimal part only of a number in JavaScript


Precision and accuracy play vital roles in numerical computations, and in the realm of JavaScript programming, the ability to extract the decimal part of a number is a crucial skill. Whether it is for rounding, comparison, or further manipulation, retrieving only the decimal part of a number can significantly enhance the precision and control over calculations. This article aims to explore a comprehensive approach to retrieving the decimal part exclusively in JavaScript, employing lesser-known techniques and rarely used functions. By mastering this technique, developers can ensure greater control over numeric operations, paving the way for more sophisticated algorithms and precise mathematical computations.

Problem Statement

Given a number in JavaScript, retrieve the decimal part only and return it as a separate number.

Sample Input −

num = 3.14159

Sample Output −

decimalPart = 0.14159

Approach

In this article, we are going to see a number of different ways to solve the above problem statement in JavaScript −

  • Using the modulo operator (%)

  • Using the Math.floor() and subtraction

  • Using the toString() and split() methods

  • Using the substring() method

Method 1: Using the Modulo Operator (%)

This approach utilizes the modulo operator (%) in JavaScript. When we divide a number by 1 and calculate the remainder, the result is effectively the decimal part of the number. By applying the modulo operator with 1 (number % 1), we can obtain only the decimal part of the given number.

Example

This code defines a function called getDecimalPart that takes a number as input. The % operator calculates the remainder when number is divided by 1. Since the integer part of the number is effectively divided by 1 and discarded, the result is the decimal part of the number.

function getDecimalPart(number) {
   return number % 1;
}
const number = 89.33;
console.log(getDecimalPart(number));

Output

The following is the console output −

0.3299999999999983

Method 2: Using the Math.floor() and Subtraction

This approach involves using the Math.floor() function in JavaScript. Math.floor() rounds down a given number to the nearest integer. By subtracting the floored value from the original number, we can isolate and retrieve only the decimal part of the number.

Example

In this code, the getDecimalPart function receives a number as input. Math.floor(number) returns the largest integer less than or equal to number, effectively removing the decimal part. Subtracting this floored value from the original number gives us only the decimal part.

function getDecimalPart(number) {
   return number - Math.floor(number);
}
const number = 89.33;
console.log(getDecimalPart(number));

Output

The following is the console output −

0.3299999999999983

Method 3: Using the ToString() and Split() Methods

This approach converts the given number to a string representation using the toString() method. Next, we use the split('.') method to split the string at the decimal point. The resulting array will have two elements: the integer part and the decimal part. By accessing the second element of the array (split('.')[1]), we can obtain the decimal part. Finally, parseFloat() is used to convert the decimal part back to a number if needed.

Example

This code converts the number to a string using number.toString(). Then, split('.') is used to split the string at the decimal point, resulting in an array. The second element of the array, split('.')[1], represents the decimal part. Finally, parseFloat() converts the decimal part back to a number.

function getDecimalPart(number) {
   return parseFloat(number.toString().split('.')[1]);
}

const number = 89.33;
console.log(getDecimalPart(number));

Output

The following is the console output −

33

Method 4: Using the Substring() Method

This approach converts the number to a string using the toString() method. We then find the index of the decimal point in the string representation using the indexOf('.') method. By using the substring() method and starting from the index of the decimal point plus one (indexOf('.') + 1), we can extract the characters after the decimal point. Finally, parseFloat() is used to convert the resulting substring back to a number, if necessary.

Example

The getDecimalPart function takes a number as input. number.toString().indexOf('.') finds the index of the decimal point in the string representation of the number. substring() is then used to extract the characters after the decimal point, starting from indexOf('.') + 1. Finally, parseFloat() is used to convert the resulting substring back to a number.

function getDecimalPart(number) {
   return parseFloat(number.toString().substring(number.toString().indexOf('.') + 1));
}

const number = 89.33;
console.log(getDecimalPart(number));

Output

The following is the console output −

33

Conclusion

To conclude, exploring the extraction of the decimal part exclusively from a number in JavaScript offers an intriguing avenue for precise mathematical manipulations. This oft-overlooked aspect of numeric processing can provide a nuanced approach to data analysis and computation, enabling developers to unravel intricate patterns and unlock hidden insights. By incorporating these lesser-known techniques into their codebase, programmers can enhance the accuracy and sophistication of their applications, resulting in more refined and tailored user experiences. Embracing the esoteric realm of decimal extraction in JavaScript can elevate the realm of numerical operations and empower developers to transcend the conventional boundaries of computation.

Updated on: 04-Aug-2023

463 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements