Get the Decimal Part of a Number in JavaScript

To get the decimal part of a floating-point number, you can use the toString() and split() methods. The toString() method converts a given value to a string, whereas the split() method splits a string at a given separator.

You can follow the below steps to get the decimal part of a number:

  1. Convert the given number to a string using the toString() method
  2. Split the string at the decimal point(.). It returns an array of two elements.
  3. Get the second element of the array i.e. element at index 1. It is the decimal part of the number but as a string.
  4. Convert the decimal string back to number using the Number() constructor.
let num = 10.3451;

// Convert number to string
const str = num.toString();

// Split above string at decimal point(.)
const arr = str.split('.');

// Get the second element and convert to number
let decimal = Number(arr[1]);

console.log(decimal);         // 3451
console.log(typeof decimal);  // number

How does it work?

In the very first step, we converted the given number to its equivalent string using the toString() method. The toString() method converts a given value to its string representation:

let num1 = 10.8976;
console.log(num1.toString());  // '10.8976'

let num2 = 3.51;
console.log(num2.toString());  // '3.51'

Once the number is converted to a string, we can split it at decimal point(.) using the String.split() method. The split() method splits the string into an array at a given separator.

split(separator);

In our example, we have to split the string at decimal places. This will result in an array of two number strings, first the integer part, and second, the decimal part.

console.log('10.8976'.split('.'));  // ["10", "8976"]
console.log('3.51'.split('.'));     // ["3", "51"]
console.log('0.32'.split('.'));     // ["0", "32"]

The second element of each array i.e. element at index 1 is the decimal part of the number. Therefore, we have to get it using array index 1.

However, the decimal part is in the form of a string. Therefore, we have to convert it back to number using the Number() constuctor.

Extending the functionality of the above approach:

The approach that we discussed above works perfectly for decimal-point numbers. But, for integer values, it doesn’t give the correct output.

This is because integer values like 10 or 20 don’t have a decimal point. Therefore, when we try to split their string equivalents, we get only a single item array which contains only the integer part eg. [“10”], [“20”], etc.

Therefore, we have to handle integer values separately.

To check if a number is an integer or not we can use the Number.isInteger() method. The isInteger() method returns true if the given number is an integer, otherwise, it returns false.

function getDecimal(num){
    if(Number.isInteger(num)){
        return 0;
    }
    
    let decimalStr = num.toString().split('.')[1];
    let decimal = Number(decimalStr);
    
    return decimal;
}

console.log(getDecimal(10.3451));   // 3451
console.log(getDecimal(10));        // 0
console.log(getDecimal(10.00));     // 0
console.log(getDecimal(-10.32));    // 32

The above approach handles both types of numbers perfectly. If the number is an integer, the getDecimal() function directly returns 0, otherwise, it returns the decimal part of the number.

Notice that the getDecimal() function returns 0 for numbers like 10.00, 5.00, etc. This is because such numbers are also considered integers eg. 5.00 is same as 5.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.