Users Online

· Guests Online: 46

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Functions - JavaScript String to Float

JavaScript String to Float

JavaScript String to Float

Introduction to Javascript string to float

Conversion of String to Float is generally used if the string including float numbers is to perform mathematical operations. When you get text field or text field data, you must receive data entered as a string.  Let assume if the data we have in float and we want to convert in the string or we have data in the string and want to convert in float so we have two methods. In this topic, we are going to learn about JavaScript String to Float.

How to convert string to float in JS using various methods?

There are 2 methods to convert string to float which are as follows

  1. Type conversion
  2. Parsefloat

So now we will see these two methods one by one

Method 1

This method uses the JavaScript Type Conversion function to convert the string type to float.

Example #1

Code:

<script>
// Javascript script to convert the string value to float Value
// Function to convert the string to float value
function convert_to_float(b) {
// Type conversion of string to float
var floatValue = +(b);
// Return float value
return floatValue;
}
//Driver code
var q = "45.658";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
var q = "-66.586";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
</script>

Output:

JavaScript String to Float output 1

Example #2

Code:

<script>
// Javascript script to convert the string value to float Value
// Function to convert the string to float value
function convert_to_float(b) {
// Type conversion of string to float
var floatValue = +(b);
// Return float value
return floatValue;
}
//Driver code
var q = "abc.658";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " + q + "</br> Type of " + q + " = " +typeof q + "<br>");
var q = "-52.586";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
</script>

Output:

JavaScript String to Float output 2

Method 2

By using the parseFloat() method:

We use the parseFloat method which is an embedded function in JavaScript to accept the string and transform it into a floating dot number. If there is no numeric value in the string, or if the first string character is not a number, then the string returns NaN value, i.e. not a number.

Example #1

Code:

<script>
// Javascript script to convert the string to float value
// Function to convert the string value to float value
function convert_to_float(b) {
// Using parseFloat() method
var floatValue = parseFloat(b);
// Return float value
return floatValue;
}
//Driver code
var q = "999.56";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
var q = "-758.44";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
</script>

Output:

JavaScript String to Float output 3

Example #2

In this example, you will able see that if we don’t input the value then how output come.

Code:

<script>
// Javascript script to convert the string to float value
// Function to convert the string value to float value
function convert_to_float(b) {
// Using parseFloat() method
var floatValue = parseFloat(b);
// Return float value
return floatValue;
}
//Driver code
var q = "ab.bc";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
var q = "-758.44";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
</script>

Output:

output 4

Example #3

In this example we will see the special case which is:

In the French float, the number is written by using the comma instead of Dot.

Code:

<script>
// Javascript script to convert the string to float value
// Function to convert  the string to float value
function convert_to_float(d) {
// Using parseFloat() method and using replace() method  to replace ', ' with '.' because in French float number is written in ','
var floatValue = parseFloat(d.replace(/, /, '.'));
// Return float value
return floatValue;
}
//Driver code
var q = "335, 586";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
var q = "-336, 11";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
</script>

Output:

output 5

Example #4

Code:

<script>
// Javascript script to convert the string to float value
// Function to convert  the string to float value
function convert_to_float(d) {
// Using parseFloat() method and using replace() method  to replace ', ' with '.' because in French float number is written in ','
var floatValue = parseFloat(d.replace(/, /, '.'));
// Return float value
return floatValue;
}
//Driver code
var q = "abc, mno";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
var q = "-336, 11";
// Call function
q = convert_to_float(q);
// Print result
document.write("Converted value = " +
q + "</br> Type of " + q + " = "
+typeof q + "<br>");
</script>

Output:

output 6

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.14 seconds
10,843,275 unique visits