How to get decimal portion of a number using JavaScript ?
Given a float number, The task is to separate the number into the integer and decimal part using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are few methods discussed.
-
Method 1: Using split() mathod.
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 splitted items.
Example 1: This example first converts the number to string then remove the portion before the decimal using split() method.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Get decimal portion of a number. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var n = -2.50999974435; up.innerHTML = n; var down = document.getElementById('GFG_DOWN'); function GFG_Fun() { down.innerHTML = (n + "").split(".")[1]; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Method 2: Using 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 parameter to its nearest integer in Downward direction of rounding i.g towards the lesser value.
Example 2: 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.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Get decimal portion of a number. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var n = 2.57; up.innerHTML = n; var down = document.getElementById('GFG_DOWN'); function GFG_Fun() { n = Math.abs(n) down.innerHTML = n - Math.floor(n); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to remove portion of a string after certain character in JavaScript ?
- Round off a number upto 2 decimal place using JavaScript
- How to convert decimal to hex in JavaScript ?
- How to remove portion of a string after a certain character in PHP?
- How to restrict input box to allow only numbers and decimal point JavaScript?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- JavaScript | Number.MAX_VALUE & Number.MIN_VALUE with Examples
- How to convert a float number to the whole number in JavaScript?
- Number.isNaN() In JavaScript
- JavaScript | Number isSafeInteger()
- Javascript | Number() Function
- Factorial of a number using JavaScript
- Number valueOf( ) Method In JavaScript
- JavaScript | Number constructor Property
- JavaScript | Pad a number with leading zeros
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



