Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Retrieving the decimal part only of a number in JavaScript
Precision and accuracy play vital roles in numerical computations, and in 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.
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
Methods Overview
We'll explore four different approaches to solve this problem:
Using the modulo operator (%)
Using Math.floor() and subtraction
Using toString() and split() methods
Using the substring() method
Method 1: Using the Modulo Operator (%)
The modulo operator (%) calculates the remainder when dividing by 1. Since any number divided by 1 leaves only its decimal part as the remainder, this is the most efficient approach.
function getDecimalPart(number) {
return number % 1;
}
const number = 89.33;
console.log("Original number:", number);
console.log("Decimal part:", getDecimalPart(number));
// Test with different numbers
console.log("Decimal part of 3.14159:", getDecimalPart(3.14159));
console.log("Decimal part of 5:", getDecimalPart(5));
Original number: 89.33 Decimal part: 0.3299999999999983 Decimal part of 3.14159: 0.14159000000000968 Decimal part of 5: 0
Method 2: Using Math.floor() and Subtraction
This method subtracts the integer part (obtained using Math.floor()) from the original number to isolate the decimal portion.
function getDecimalPart(number) {
return number - Math.floor(number);
}
const number = 89.33;
console.log("Original number:", number);
console.log("Integer part:", Math.floor(number));
console.log("Decimal part:", getDecimalPart(number));
// Handle negative numbers
console.log("Decimal part of -7.25:", getDecimalPart(-7.25));
Original number: 89.33 Integer part: 89 Decimal part: 0.3299999999999983 Decimal part of -7.25: -0.25
Method 3: Using toString() and split() Methods
This approach converts the number to a string, splits it at the decimal point, and extracts the decimal digits. Note that this method returns just the decimal digits, not as a decimal number.
function getDecimalPart(number) {
const parts = number.toString().split('.');
return parts[1] ? parseFloat('0.' + parts[1]) : 0;
}
// Alternative version that returns just the digits
function getDecimalDigits(number) {
const parts = number.toString().split('.');
return parts[1] || '0';
}
const number = 89.33;
console.log("Original number:", number);
console.log("Decimal as number:", getDecimalPart(number));
console.log("Decimal digits:", getDecimalDigits(number));
// Test with integer
console.log("Integer 5 decimal part:", getDecimalPart(5));
Original number: 89.33 Decimal as number: 0.33 Decimal digits: 33 Integer 5 decimal part: 0
Method 4: Using the substring() Method
This method finds the decimal point's position and extracts everything after it using substring().
function getDecimalPart(number) {
const str = number.toString();
const decimalIndex = str.indexOf('.');
if (decimalIndex === -1) {
return 0; // No decimal point found
}
const decimalDigits = str.substring(decimalIndex + 1);
return parseFloat('0.' + decimalDigits);
}
const number = 89.33;
console.log("Original number:", number);
console.log("Decimal part:", getDecimalPart(number));
// Test edge cases
console.log("Decimal part of 0.005:", getDecimalPart(0.005));
console.log("Decimal part of 100:", getDecimalPart(100));
Original number: 89.33 Decimal part: 0.33 Decimal part of 0.005: 0.005 Decimal part of 100: 0
Comparison
| Method | Performance | Handles Negatives | Floating Point Issues |
|---|---|---|---|
| Modulo (%) | Fastest | Yes | Yes |
| Math.floor() | Fast | Yes | Yes |
| String split() | Slower | Yes | No |
| substring() | Slower | Yes | No |
Key Points
The modulo operator (%) is the most efficient method for extracting decimal parts
Floating-point precision issues may occur with mathematical operations
String-based methods avoid floating-point errors but are slower
All methods handle negative numbers, but behavior may vary
Conclusion
The modulo operator (%) provides the most efficient way to extract decimal parts in JavaScript. While string-based methods offer precision advantages, mathematical approaches are generally preferred for performance-critical applications.
