# How to get the decimal part of a floating number in JavaScript?

In this article, we will learn **how to get the decimal part of a floating number in JavaScript**?

Submitted by Abhishek Pathak, on October 28, 2017

Floating values are part of number system in programming languages. As the computation is handled by machine, so the precision is increased and can produce reliable results. In this article we will see **how to get the decimal part of the floating number in JavaScript**?

Subtracting two floating values is not as simple as subtracting two numbers. When it comes to floating values, the JavaScript can handle precision upto 16 decimal units. The smallest number in JavaScript is 5e-324. It is like the smallest block that adds up to make numbers and therefore, we can't reach a perfect floating result when performing floating operations. Keeping this in mind, we will see how to get the decimal part of the floating number.

**Here is the code,**

function getDecimal(n) { return (n - Math.floor(n)); } console.log(5.224); //Output: 0.2240000000000002

In this code, we are creating a function that will accept the floating number from the argument **'n'** and use this value in the function. *Now*, **to return just the decimal part we have to subtract the number counterpart before decimal point from the number. This will return the values right to the decimal point. Finally we console log the output**.

Note that the value returned is not exact, and approximate upto 16 decimal places. Since the smallest value in JavaScript is close to zero, but still large in very minute scale, therefore it will not result in exact precision. That is why we get the strange result which is close to actual result or approximate but not accurate.

Hope you like this article; please share your thoughts in the comments below.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.