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.
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
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