

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a string to a floating point number in JavaScript?
Javascript has provided a method called parseFloat() to convert a string into a floating point number. Floating numbers are nothing but decimals. We also have another method called parseInt() to do the same task but it will not deal with decimals. It returns only integers.
ParseFloat() can change a number string into a number whereas if any string other than a number is sent then it gives NaN as output.
syntax
parseFloat(Value);
It takes a number string as input and returns a floating point number as output.
Example
In the following example, parseFloat() has given floating number as output, whereas parseInt() has given an integer as output.
<html> <body> <script> var a = parseFloat(" 100 "); document.write(a +"</br>"); var b = parseFloat("120.65") document.write(b +"</br>"); var c = parseInt("3.14"); document.write(c +"</br>"); </script> </body> </html>
Output
100 120.65 3
In some cases there are both a number and also a string in a variable then, if the number is at first place then only the number will be returned as output, splicing out the string part, else NaN will be returned as output.
Example
<html> <body> <script> var a = parseFloat(" hello ") document.write(a +"<br>"); var b = parseFloat("hello1234") document.write(b +"<br>"); var c = parseFloat("3.14hello"); document.write(c +"<br>"); </script> </body> </html>
Output
NaN NaN 3.14
- Related Questions & Answers
- Convert a floating point number to string in C
- How to deal with floating point number precision in JavaScript?
- How to count set bits in a floating point number in C?
- Fixed Point and Floating Point Number Representations
- Convert the specified double-precision floating point number to a 64-bit signed integer in C#
- Format floating point number in Java
- How to convert a string with zeros to number in JavaScript?
- How to convert Number to String in JavaScript?
- How to convert String to Number in JavaScript?
- How to convert a value to a string in JavaScript?
- How to convert a value to a number in JavaScript?
- How to convert a string to JavaScript object?
- Reinterpret 64-bit signed integer to a double-precision floating point number in C#
- Convert string to a number in Java
- How to convert a string in to a function in JavaScript?