How to get decimal portion of a number using JavaScript ?

Given a float number, The task is to separate the number into integer and decimal parts using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are a few methods discussed. 

These are the following methods:

Table of Content

  • Javascript String split() Method
  • JavaScript Math.abs( ) and Math.floor( )
  • Javascript indexOf() method
  • Using toFixed() Method

Javascript String split() Method

This method is used to split a string into an array of substrings and returns the new array. 

Syntax: 

string.split(separator, limit);

Parameters:

  • separator: This parameter is optional. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item) 
  • limit: This parameter is optional. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array. 

Return value:

Returns a new Array, having the split items.

Example: This example first converts the number to string then removes the portion before the decimal using the split() method

Javascript
let n = -2.50999974435;
console.log(n);

function GFG_Fun() {
    console.log((n + "").split(".")[1]);
}
GFG_Fun()

Output
-2.50999974435
50999974435

JavaScript Math.abs( ) and Math.floor( )

  • Math.abs( ): The Math.abs() function in JavaScript is used to return the absolute value of a number. It takes a number as its parameter and returns its absolute value.
  • Math.floor( ): The Math.floor() function in JavaScript is used to round off the number passed as a parameter to its nearest integer in a Downward direction of rounding i.g towards the lesser value.

Example: This example subtracts the floor of the number by original number to get the decimal portion. But in this case, we’ll get the exact portion after decimal. We’ll get the approximate result.

Javascript
let n = 2.57;
console.log(n);

function GFG_Fun() {
    n = Math.abs(n)
    console.log(n - Math.floor(n));
}
GFG_Fun()

Output
2.57
0.5699999999999998

Javascript indexOf() method

Using the indexOf() method to get the decimal part of the given number, in which we convert our number into a string and then get the index of the decimal point with the help of the indexOf() method. and then extract the part of the decimal string as a substring.

Syntax:

number.toString().indexOf(".")

Example:  In this example first we convert to the string and then get the index of the decimal point and store it in a new variable, and again convert the number into a string and get the decimal part using the substring() method.

Javascript
let number = 20.125;
let decimalValue = number.toString().indexOf(".");
let result = number.toString().substring(decimalValue+1);
//orignal number
console.log(number)
//Decimal part of Number
console.log(result);

Output
20.125
125

Using toFixed() Method

The toFixed() method formats a number using fixed-point notation and returns a string representation of the number. This method can be useful for obtaining the exact decimal part of a number by specifying the number of digits after the decimal point.

Syntax:

number.toFixed(digits);

Parameters:

  • ‘digits’: The number of digits to appear after the decimal point. This parameter is optional and defaults to 0.

Example: In this example, we use the toFixed() method to format the number to a fixed decimal point, convert it to a string, and then extract the integer and decimal parts separately.

JavaScript
let number = 15.6;

function splitNumber(number) {
    let fixedNumber = number.toFixed(10); 
    let parts = fixedNumber.split('.'); 
    let integerPart = parseInt(parts[0]); 
    let decimalPart = parseFloat('0.' + parts[1]);
    return { integerPart, decimalPart };
}

let result = splitNumber(number);

console.log(result.integerPart); 
console.log(result.decimalPart); 

Output
15
0.6




Contact Us