# Which Function Is Used to Convert String Data to Float Type?

//

Heather Bennett

When working with data in programming, it is common to encounter situations where you need to convert a string into a floating-point number. In JavaScript, the function used to perform this conversion is parseFloat().

## The parseFloat() Function

The `parseFloat()` function is a built-in JavaScript function that parses a string and returns a floating-point number. It attempts to convert as much of the given string as possible into a number.

### Syntax:

The basic syntax of the `parseFloat()` function is:

``parseFloat(string)``

### Example:

Let’s take an example to understand how the `parseFloat()` function works.

``````// Example 1
let num = parseFloat('10.5');
console.log(num); // Output: 10.5

// Example 2
let num2 = parseFloat('20');
console.log(num2); // Output: 20``````

In Example 1, the `parseFloat()` function converts the string ‘10.5’ into the floating-point number 10.5 and assigns it to the variable `num`. The console then logs the value of `num`, which outputs 10.5.

In Example 2, the `parseFloat()` function converts the string ’20’ into the floating-point number 20 and assigns it to the variable `num2`. The console logs the value of `num2`, which outputs 20.

## Parsing Limitations

The parseFloat() function has a few limitations that are important to keep in mind.

• If the string starts with a non-numeric character or does not contain any numeric characters, the function will return NaN (Not-a-Number).
• The function stops parsing as soon as it encounters an invalid character. For example, if the string is ‘10.5abc’, the function will only convert ‘10.5’ and ignore the rest of the characters.
• The function does not handle exponential notation. If you need to parse numbers in scientific notation, you should use the `Number()` constructor instead.

### Example:

Let’s look at some examples to understand these limitations better.

``````// Example 1
let result1 = parseFloat('abc');
console.log(result1); // Output: NaN

// Example 2
let result2 = parseFloat('10.5abc');
console.log(result2); // Output: 10.5

// Example 3
let result3 = parseFloat('1e5');
console.log(result3); // Output: 1``````

In Example 1, since the string ‘abc’ is not a valid number, the `parseFloat()` function returns NaN.

In Example 2, even though the string ‘10.5abc’ contains invalid characters after ‘10.5’, the function successfully converts ‘10.5’ into a number and ignores the rest of the characters.

In Example 3, since `parseFloat()` does not handle exponential notation, it stops parsing at ‘1’ and returns that value instead of converting it into scientific notation (1e5 = 100000).

## Conclusion

The `parseFloat()` function in JavaScript is used to convert a string into a floating-point number. It is important to be aware of the limitations of this function, such as its inability to handle non-numeric characters at the beginning of a string or exponential notation. However, when used correctly, `parseFloat()` can be a powerful tool for converting string data into the desired numeric format.

By understanding and utilizing the `parseFloat()` function effectively, you can confidently convert string data to float type in your JavaScript programs.

I hope this article has provided you with a clear understanding of the `parseFloat()` function in JavaScript. Happy coding!