Why we need parsefloat function?
Web pages are very interactive. To know your BMI you have to enter your height and weight. For online shopping you have to provide details like phone numbers, address, and your credit card number. In some cases, the entries have to be considered as numbers and used for mathematical calculations like in BMI and in other cases they have to be considered as texts like your phone number or your house number. How does the web page handle all this data?
Web pages are designed with HTML and have text boxes where users can enter their data. There is no concept of number boxes and so all the data that is entered, be it numbers or letters are considered as texts only.
It is the job of the code to identify text boxes with numbers and convert them from text to number format so that mathematical calculations can be performed on them.
The parsefloat converts strings into floating point numbers so that you can perform mathematical operations on them.
How to use parsefloat function?
To understand how parsefloat works you have to understand the concept of floating point numbers. Let’s do that first.
A lot of theory goes into how computer systems interpret numbers. The number 45 is a whole number or an integer as it doesn’t have any decimal part. However, real numbers can contain decimal part and the computer uses the concept of floating point to handle them.
Floating point number means that there is no fixed number of digits either before or after the decimal point. When represented as a number, approximation is used.
The syntax for parsefloat is parseFloat (). In the bracket you have to specify the string that has to be parsed. The function will return a floating point number.
Here is a code sample that used parseFloat ().
Here the web page prompts the user to enter a string in the text box and uses parseFloat to parse it and returns the result as a floating number.
var num1 = prompt(“enter a number”) var answer = parseFloat(num1) Alert(“the number you entered is:” + answer)
Now, let me explain the code.
answer is another variable that stores the parsed value of num1. So, in the answer, 25 is stored as number 25.
Alert is a function that shows messages on the screen. In this case it will show, “the number you entered is 25”
If you enter 25.44, with parseFloat it will be shown as 25.44.
Why do you need parseFloat?
Suppose you want to add two numbers, and you type this code.
var num1 = prompt(“enter a number”) var num2 = prompt(“enter a number”) var answer = num1 +num2 Alert(“sum is:” + answer)
If the user enters 3 and 8, you will not get 11 as an answer but you will get 38. This is because the + sign is used to add numbers as well string texts together. So the system appends 3 to 8 and gives you 38.
The right code would be
var num1 = prompt(“enter a number”) var num2 = prompt(“enter a number”) var answer = parseFloat(num1) +parseFloat(num2) Alert(“sum is:” + answer)
Now, you get 11 as an answer.
How parseFloat() works?
The function parses the string for numerals, signs(+,-) or decimal points and even exponents. It stops when it encounters anything other than these. So, if the user enters 99cents, it will return 99. It can also handle leading and trialing spaces. However, if the user types cents99, the parseFloat() will return NaN whcih means it could not perform the operation.
If the user types 3.15e-2, it will return 3.15.
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.