. Advertisement .
..3..
. Advertisement .
..4..
The parseFloat() function provides one of the essential capabilities, and a good understanding of it isn’t optional for JavaScript developers. Follow this guide to learn how to use parseFloat() in JavaScript.
parseFloat() In JavaScript
The main and only task of the parseFloat() in JavaScript is to convert an argument into a floating-point number.
Syntax:
parseFloat(value);
The function needs a parameter, which can be a string or any object. parseFloat() then parses and converts it into a string if needed using the ToString operation before returning a floating-point number.
Here are some examples that can illustrate the capabilities and limitations of the parseFloat() function.
Example:
console.log(parseFloat(4.567));
console.log(parseFloat("4.567"));
console.log(parseFloat(" 4.567 "));
console.log(parseFloat(10/3))
console.log(parseFloat("ITTutoria"))
console.log(parseFloat("23.3d"))
Output:
4.567
4.567
4.567
3.3333333333333335
NaN
23.3
You can feed a float-point number into the parseFloat() function without or without a quote, and it still yields the same result. Remember that all the leading and trailing whitespace characters are ignored by parseFloat().
This function also understands expressions like 10/3. But when you feed an argument with no number in it, the expected return is NaN.
The parseFloat() function stops processing its argument the moment it runs into a non-numeric character. That is why the output of 23.3d is 23.3.
If you give parseFloat() two decimals, it will ignore the second one and the remainder of the argument.
console.log(parseFloat("21.06.22"))
Output:
21.06
An object can be parsed into this function as long as it has a toString() method.
const object = {
toString: () => "192.168",
};
console.log(parseFloat(object));
Output:
192.168
You can incorporate parseFloat() with other JavaScript to carry out more complicated tasks.
Example:
var x = parseFloat("3.1417");
if (isNaN(x))
console.log("x is not a number");
else
console.log("x is a number");
var y = parseFloat("howareyou");
if (isNaN(y))
console.log("y is not a number");
else
console.log("y is a number");
Output:
x is a number
y is not a number
Using the isNaN() function, you can verify the results produced by the parseFloat() function when it is given a real floating-point number and a string with no numeric characters.
Example:
console.log(parseFloat("885e-9"))
console.log(parseFloat("885e9"))
Output:
8.85e-7
885000000000
In this snippet, parseFloat() helps us filter out input from the user.
const pa=parseFloat(prompt("Enter Principal Amount: "));
const rate=parseFloat(prompt("Enter Rate:"));
const year=parseFloat(prompt("Enter Number Of Years:"));
var s_interest=(pa*rate*year)/100;
console.log("Simple Interest annually is: ", s_interest);
Output (when we enters 1111A, 2, 5):
Simple Interest annually is: 111.1
You can see that thanks to parseFloat(), the code has no trouble understanding our input and assigning 1111 as the principal amount instead of giving an error.
Conclusion
parseFloat() in JavaScript can convert an argument into a floating-point number. Remember that you can provide this argument in different ways, and the results will depend on it.
Leave a comment