In this article, we will convert a string into a float in Javascript. We can convert a string into a float in JavaScript by using some methods which are described below:
Methods to Concert String into Float:
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.
Example: Below program demonstrates the above approach
javascript
function convert_to_float(a) {
let floatValue = +a;
return floatValue;
}
let n = "55.225" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
n = "-33.565" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
|
OutputConverted value = 55.225 Type of 55.225 = number
Converted value = -33.565 Type of -33.565 = number
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.
Example: Below program demonstrates the above approach
javascript
function convert_to_float(a) {
let floatValue = parseFloat(a);
return floatValue;
}
let n = "245.165" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
n = "-915.55" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
|
OutputConverted value = 245.165 Type of 245.165 = number
Converted value = -915.55 Type of -915.55 = number
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.
Example: Below program demonstrates the above approach
javascript
function convert_to_float(a) {
let floatValue = parseFloat(a.replace(/, /, "." ));
return floatValue;
}
let n = "245, 165" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
n = "-915, 55" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
|
OutputConverted value = 245.165 Type of 245.165 = number
Converted value = -915.55 Type of -915.55 = number
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. Example: Below program demonstrates the above approach
Javascript
function convert_to_float(a) {
let floatValue = eval(a);
return floatValue;
}
let n = "55.225" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
n = "-33.565" ;
n = convert_to_float(n);
console.log( "Converted value = " + n +
" Type of " + n + " = " + typeof n);
|
OutputConverted value = 55.225 Type of 55.225 = number
Converted value = -33.565 Type of -33.565 = number