JavaScript Parse Float From String

JavaScript Parse Float From String | JavaScript is a good degree, dynamically written, and understood client-side programming language. A built-in method called parseFloat() in JavaScript allows us to parse strings and gives a floating-point value as a result.

The parseFloat() technique estimates whether the string’s initial element is numeric or not and then continues to parse the argument until the ending. If the string’s initial element is not a number, parseFloat() returns NaN (Not a Number). The entire text is parsed by the parseFloat() function. The result is a floating-point value with 5 digits after the decimal if the provided input contains 5 digits after the decimal.

Syntax of parseFloat() method: parseFloat(str) 
Parameters:
String: The float that must be processed in text form.
Output value: If the string’s first element is a number, it yields the interpreted floating-point number; otherwise, it gives NaN.

JavaScript Parse Float Example

var num1 = parseFloat("90.49023");
console.log("Number: " + num1);

var num2 = parseFloat("ABCD");
console.log("Number: " + num2);

var num3 = parseFloat("KnowProgram2025");
console.log("Number: " + num3);

var num4 = parseFloat("2025");
console.log("Number: " + num4);

Output:-

Number: 90.49023
Number: NaN
Number: NaN
Number: 2025

We can also round the floating point value. The toFixed() technique is employed to restrict the number of digits up to N positions after the decimal. The float is rounded up to N spaces after the decimal using the toFixed() technique.

Syntax of toFixed() method: toFixed(int)
Parameter:
int: The number of digits after the decimal place that needs to be read from the string.
Return value: The number or string, leveled to the specific spots after the decimal, is returned as the return type. The result is padding by 0 to retain the number of digits after decimal in the output if the requested value is greater than the number of digits after decimals in the real string.

var num1 = parseFloat("90.49023");
var num2 = parseFloat("90.49023").toFixed(2);
console.log("Without using toFixed() method: " + num1);
console.log("Using toFixed() method: " + num2);

Output:-

Without using toFixed() method: 90.49023
Using toFixed(2) method: 90.49

The number is rounded up to two spaces after the decimal in the example above. But occasionally, the developer could decide without rounding the figure. This is an alternative approach to rounding the floating-point number to the required precision.

This example defines the function ParseFloat(), which takes two inputs. A text or number to be parsed is given as the first argument, and the number of positions after the decimal point is given as the second parameter.

JavaScript Parse Float Example-2

If the provided argument is not already in string format, the first parameter needs to be transformed into a string. The string is now cut from the beginning up to the number of places after the decimal that was supplied. The resultant sliced string is then type converted to an integer. This method’s drawback is that, in contrast to parseFloat(), it is unable to determine whether the provided string genuinely includes a floating-point integer.

function ParseFloat(str, val) {
   str = str.toString();
   str = str.slice(0, (str.indexOf(".")) + val + 1);
   return Number(str);
}
console.log(ParseFloat("74.34484394", 2))

Output:-

74.34

JavaScript is most commonly used for creating web pages, but it is also utilized in many other non-browser environments.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *