Get the Decimal Part of a Number in JavaScript


Borislav Hadzhiev

Mon Oct 25 20212 min read

Get the Decimal Part of a Number #

To get the decimal part of a number:

  1. Check if the number is an integer, if it is, return 0.
  2. Otherwise, convert the number to a string and use the split() method to split it on the dot.
  3. Convert the array element at index 1 to a number and return the result.
const num = 12.345; 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

We created a reusable function that returns the decimal part of a number.

We used the Number.isInteger method to determine 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.

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 and JavaScript automatically drops any insignificant trailing zeros from numbers.
console.log(5.00 === 5); // ๐Ÿ‘‰๏ธ true

After we find out that 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.

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.

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

An alternative approach is to use the modulo operator.

To get the decimal part of a number use the modulo (%) operator to get the remainder from dividing the number by 1, e.g. 3.17 % 1. The modulo operator returns the remainder of dividing one number by another.

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

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.

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.

My personal preference is to use the first approach, which splits the number on the dot and returns the result. You might have to split on a comma, depending on the locale, but this is very easy to handle in your code.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee