Get the Decimal Part of a Number in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Get the Decimal Part of a Number in JavaScript

To get the decimal part of a number:

  1. Use the toString() method to convert the number to a string.
  2. Use the split() method to split the string on the dot.
  3. Convert the array element at index 1 to a number to get the decimal part.
index.js
function getDecimalPart(num) { if (Number.isInteger(num)) { return 0; } const decimalStr = num.toString().split('.')[1]; return Number(decimalStr); } console.log(getDecimalPart(12.345)); // ๐Ÿ‘‰๏ธ 345 console.log(getDecimalPart(-1.23)); // ๐Ÿ‘‰๏ธ 23 console.log(getDecimalPart(1)); // ๐Ÿ‘‰๏ธ 0

get decimal part of number

The code for this article is available on GitHub

The getDecimalPart function takes a number as a parameter and returns the decimal part of the number.

If you need to get the number of digits after the decimal, use the following function instead.

index.js
function digitsAfterDecimal(num) { if (Number.isInteger(num)) { return 0; } const arr = num.toString().split('.'); return arr[1].length; } console.log(digitsAfterDecimal(1)); // ๐Ÿ‘‰๏ธ 0 console.log(digitsAfterDecimal(1.0)); // ๐Ÿ‘‰ 0 console.log(digitsAfterDecimal(1.1)); // ๐Ÿ‘‰๏ธ 1 console.log(digitsAfterDecimal(1.123)); // ๐Ÿ‘‰๏ธ 3 console.log(digitsAfterDecimal(1.1234)); // ๐Ÿ‘‰๏ธ 4
The code for this article is available on GitHub

The digitsAfterDecimal function takes a number as a parameter and returns the number of digits after the decimal.

We used the Number.isInteger() method to check if the passed-in number is an integer.

If the number is an integer, we return 0 straight away as it has no decimal part.

Here are some examples of using the Number.isInteger() method.

index.js
console.log(Number.isInteger(5)); // ๐Ÿ‘‰๏ธ true console.log(Number.isInteger(-5)); // ๐Ÿ‘‰๏ธ true console.log(Number.isInteger(5.0)); // ๐Ÿ‘‰๏ธ true console.log(Number.isInteger(5.5)); // ๐Ÿ‘‰๏ธ false console.log(Number.isInteger('5')); // ๐Ÿ‘‰๏ธ false

Notice that numbers like 5.00 are considered integers because 5.00 is the same as 5.

JavaScript automatically drops any insignificant trailing zeros from numbers.

index.js
console.log(5.00 === 5); // ๐Ÿ‘‰๏ธ true

Now that we know the number isn't an integer, we convert it to a string and call the String.split() method on it to split it on the dot.

The split method takes a separator as a parameter and splits the string into an array of substrings.

index.js
console.log('1.37'.split('.')); // ๐Ÿ‘‰๏ธ ['1', '37'] console.log('-3.58'.split('.')); // ๐Ÿ‘‰๏ธ ['-3', '58']

The decimal part of the number is stored in the array at index 1. However, we have to convert it back to a number before returning from the function.

index.js
function getDecimalPart(num) { if (Number.isInteger(num)) { return 0; } const decimalStr = num.toString().split('.')[1]; return Number(decimalStr); } console.log(getDecimalPart(12.345)); // ๐Ÿ‘‰๏ธ 345 console.log(getDecimalPart(-1.23)); // ๐Ÿ‘‰๏ธ 23 console.log(getDecimalPart(1)); // ๐Ÿ‘‰๏ธ 0
The code for this article is available on GitHub

JavaScript indexes are zero-based, so the first element in the array has an index of 0 and the last element has an index of array.length -1.

# Get the Decimal Part of a Number using the modulo % operator

An alternative approach is to use the modulo operator.

When used with a divisor of 1, the modulo operator returns the decimal part of the number.

index.js
function getDecimalPart(num) { return num % 1; } console.log(getDecimalPart(3.137)); // ๐Ÿ‘‰๏ธ 0.137 console.log(getDecimalPart(1.37)); // ๐Ÿ‘‰๏ธ 0.37000000000001 console.log(getDecimalPart(5)); // ๐Ÿ‘‰๏ธ 0

get decimal point of number using modulo

The code for this article is available on GitHub

We used the modulo (%) operator to get the remainder of dividing the number by 1.

This gives us the number after the decimal. However, floating-point numbers don't represent all decimals precisely in binary which sometimes leads to inconsistent results.

index.js
console.log(0.1 + 0.2 === 0.3); // ๐Ÿ‘‰๏ธ false

The sum of 0.1 and 0.2 is actually equal to 0.30000000000000004 instead of 0.3.

This is because the binary floating-point format cannot accurately represent numbers like 0.1 or 0.2.

The code gets rounded to the nearest number, resulting in a rounding error.

Alternatively, you can use the String.slice() method

# Get the Decimal Part of a Number using String.slice()

This is a three-step process:

  1. Use the toString() method to convert the number to a string.
  2. Use the indexOf() method to get the index of the period.
  3. Use the Strings.slice() method to get the decimal part of the number.
index.js
function getDecimalPart(num) { if (Number.isInteger(num)) { return 0; } const str = num.toString(); const indexOfDot = str.indexOf('.'); return Number(str.slice(indexOfDot + 1)); } console.log(getDecimalPart(12.345)); // ๐Ÿ‘‰๏ธ 345 console.log(getDecimalPart(-1.23)); // ๐Ÿ‘‰๏ธ 23 console.log(getDecimalPart(1)); // ๐Ÿ‘‰๏ธ 0

get decimal part of number using slice

The code for this article is available on GitHub

If the number is an integer, we return 0 right away as it doesn't have a decimal part.

The next step is to convert the number to a string and use the indexOf() method to get the index of the period.

index.js
const num = 12.345; console.log(num.toString().indexOf('.')); // ๐Ÿ‘‰๏ธ 2

The last step is to use the String.slice() method to get the portion of the string after the period.

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

When only a single argument is passed to the String.slice() method, the slice goes to the end of the string.

Notice that we added 1 to the index of the period because the start index parameter is inclusive and we want to omit the period from the result.

index.js
function getDecimalPart(num) { if (Number.isInteger(num)) { return 0; } const str = num.toString(); const indexOfDot = str.indexOf('.'); return Number(str.slice(indexOfDot + 1)); }
The code for this article is available on GitHub

When using this approach, there aren't any issues with rounding errors.

You can also use the Math.trunc() method to get the decimal part of a number.

# Get the Decimal Part of a Number using Math.trunc()

This is a two-step process:

  1. Use the Math.trunc() method to get the integer part of the number.
  2. Subtract the result from the number.
index.js
function getDecimalPart(num) { return num - Math.trunc(num); } console.log(getDecimalPart(3.137)); // ๐Ÿ‘‰๏ธ 0.137 console.log(getDecimalPart(1.234)); // ๐Ÿ‘‰๏ธ 0.23399999999999999 console.log(getDecimalPart(5)); // ๐Ÿ‘‰๏ธ 0

get decimal part of number using math trunc

The code for this article is available on GitHub

The Math.trunc() method returns the integer part of a number by removing the decimal part.

index.js
console.log(Math.trunc(5.9999)); // ๐Ÿ‘‰๏ธ 5 console.log(Math.trunc(5.0001)); // ๐Ÿ‘‰๏ธ 5

The last step is to subtract the result of calling Math.trunc() from the number.

However, when using this approach you might also encounter rounding errors when dealing with numbers that cannot be accurately represented in binary.

Which approach you pick is a matter of personal preference. I'd use the String.split() option as I find it quite direct and intuitive.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev