Converting string into float in JavaScript

Last Updated : 25 Feb, 2026

In JavaScript, a string can be converted into a floating-point number using built-in conversion functions. This is useful when working with numeric input from forms, APIs, or user data.

  • Use parseFloat(string) to convert a string into a floating-point number.
  • You can also use Number(string) to convert the string into a numeric value.
  • If the string cannot be converted, the result will be NaN (Not a Number).

We can convert a string into a float in JavaScript by using some methods which are described below:

Method 1: By using Type Conversion of JavaScript

In this method, we will use the Type Conversion feature of JavaScript which will convert the string value into float.

javascript
// Javascript script
// to convert string
// to float value

// Function to convert
// string to float value
function convert_to_float(a) {

    // Type conversion
    // of string to float
    let floatValue = +a;

    // Return float value
    return floatValue;
}

//Driver code
let n = "55.225";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
    " Type of " + n + " = " + typeof n);

n = "-33.565";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);

Method 2: By using parseFloat() Method

  • In this method, we will use the parseFloat() method which is an inbuilt function in JavaScript that is used to accept the string and convert it into a floating point number.
  • If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number. 
javascript
// Javascript script
// to convert string
// to float value

// Function to convert
// string to float value
function convert_to_float(a) {

    // Using parseFloat() method
    let floatValue = parseFloat(a);

    // Return float value
    return floatValue;
}

//Driver code
let n = "245.165";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);
n = "-915.55";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);

Special Case: In French, float numbers are written by the use of a comma (, ) as a separator instead of a dot(.) as a separator.

Example:

The value 245.67 in French is written as 245, 67

To convert a French string into a float in JavaScript we will first use replace() method to replace every (, ) with (.) then follow any of the above-described methods. 

javascript
// Javascript script
// to convert string
// to float value

// Function to convert
// string to float value
function convert_to_float(a) {
    // Using parseFloat() method
    // and using replace() method
    // to replace ', ' with '.'
    let floatValue = parseFloat(a.replace(/, /, "."));

    // Return float value
    return floatValue;
}

//Driver code
let n = "245, 165";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);

n = "-915, 55";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);

Method 3: By using the eval() function

  • In this method, we will use the eval() method which is an inbuilt function in JavaScript that is used to evaluate the string return result.
  • If the string contains a number then it converts it from string to number and then returns it and if contains other than the number it returns NaN i.e, not a number.
JavaScript
// Javascript script
// to convert string
// to float value

// Function to convert
// string to float value
function convert_to_float(a) {
    // Type conversion
    // of string to float
    let floatValue = eval(a);

    // Return float value
    return floatValue;
}

//Driver code
let n = "55.225";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);

n = "-33.565";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);

Method 4: By using Number() constructor

  • In this method, we can use the Number() constructor which is a built-in function in JavaScript to convert a string into a number, including floats.
  • It parses the argument as a float (or integer) and returns the result.
JavaScript
// Using Number() constructor to convert a string into a float
let stringNumber = "3.14";
let floatNumber = Number(stringNumber);
console.log(floatNumber); // Output: 3.14

Method 5: By using Unary Plus Operator

The unary plus (+) operator can be used to convert a string to a number, including floats. It is a simple and concise way to perform this conversion.

JavaScript
// Javascript script
// to convert string
// to float value

// Function to convert
// string to float value
function convert_to_float(a) {
    // Using unary plus operator
    let floatValue = +a;

    // Return float value
    return floatValue;
}

//Driver code
let n = "123.456";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);

n = "-789.012";

// Call function
n = convert_to_float(n);

// Print result
console.log("Converted value = " + n +
        " Type of " + n + " = " + typeof n);
Comment