JavaScript Get Integer and Decimal Portion from a Float Number
Last Updated: 2022-10-19 23:59:19In our regular development life sometimes we need to get the integer and decimal portion from a float number. In this post, I will explain an easy way to get the integer or integer portion from a float number using javascript.
A common way of getting integer and decimal Portions
let's consider we have the number 15.12 and we need to get the integer portion (15) and decimal Portion (0.12). So we can do the following steps to get our result.
- get the integer part of our number and store it in a new variable.
- subtract the integer part from our number and get the decimal portion.
var num = 15.12;
var integerNum = parseInt(num);
var fractionalPortion = num - integerNum;
console.log(fractionalPortion);
This is one of the easiest and most common ways to get the integer and the decimal portion of a float number. But the problem is This technique will not give us the exact decimal portions. For example, our example code will return 0.11999999999999922 though it should return 0.12
So we will use String Split Technique, in this technique we will follow the following steps.
- Convert our number from int to string
- Split the number by a dot(.), this will return an array with integer and decimal numbers.
- Convert the first item of the array to a Number and get the integer portion
- Convert the Second item of the array to the number and get the decimal portion.
Get the integer and the decimal portion of a number using the javascript string split technique.
var num = 15.12;
var strNum = num.toString();
var splitNums = strNum.split(".");
var integerPortion = Number(splitNums[0]);
var FractionalPortion = Number("0." + splitNums[1]);
console.log("integer: " + integerPortion);
console.log("Fractional: " + FractionalPortion);
javascript function to get the integer portion from a float number
in javascript there are many ways to get the integer part of a float number for example we can use the javascript floor() function to get the integer part.
var floatNumber = 15.12;
var integerPart = Math.floor(floatNumber);
console.log(integerPart); // will return 15
but we can create our own custom function using the javascript string split() function
function getIntegerPart(floatNumber) {
var splitNum = floatNumber.toString().split(".");
var integerPart = Number(splitNum[0]);
return integerPart;
}
console.log(getIntegerPart(15.12)); // will return 15
javascript function to get the decimal part from a float number
using the javascript split function we can get the decimal part from a float number. After the split, the second item of the array will contain our decimal part. we just need to add 0. with the second item and convert it to a Number.
function getFractionalPart(floatNumber) {
var splitNum = floatNumber.toString().split(".");
var fractionalPart = Number("0." + splitNum[1]);
return fractionalPart;
}
console.log(getFractionalPart(15.12)); // will return 0.12
Still you face problems, feel free to contact with me, I will try my best to help you.