Home » Programming » Javascript » Javascript Float Parsefloat

Converting to Float Numbers with the parseFloat() JavaScript Function, With Examples

This article will explain floating-point numbers and how to convert values to floating-point numbers using parseFloat() in JavaScript.

Looking to convert to an integer value instead – use parseInt()!

What is a Floating Point Number?

In programming, a floating-point number (commonly just called a float) is a number with any number of characters before or after a decimal point.

A floating-point number might look something like this:

14.392

floating-point number or float is also a type of variable. A variable’s type determines what kind of values it can store and what can be done with the variable (for example, math can be done with numerically typed variables, array type variables can be iterated through, etc.).

floating-point typed variable is a variable that can store a floating-point number, just as an integer type variable can store only integers.

They are an Approximation and Imprecise

When being processed by a computer, floating-point numbers are actually an approximation. This is because computers compute everything in base 2 and must store and process values as such, so many floating-point numbers will be processed as a fraction. So, naturally, this has implications for their accuracy.

We’ve put together a whole article on this that explains things properly and provides some solutions for mitigating the inaccuracies of floating-point numbers in Javascript and other programming languages.

Long story short, use floating-point numbers where appropriate. However, don’t use them when calculating currency amounts if precision is essential; you may wind up overcharging!

The JavaScript parseFloat() Function

The parseFloat() function converts a given value into a floating-point variable. It will attempt to interpret whatever is passed to it as a floating-point number and store it as such.

JavaScript parseFloat() Function Syntax

With that out of the way, here’s the syntax for the parseFloat() function:

parseFloat(NUMBER_STRING)

Note that:

  • NUMBER_STRING should be a string containing a numerical value
    • parseFloat() will attempt to convert it to a floating-point number
    • If NUMBER_STRING is not a string, it will be converted to one
    • NUMBER_STRING can contain exponent notation
  • A numerical value of the float type will be returned
    • If non-numeric characters are encountered before any interpretable number, NaN will be returned
    • Leading white space is, however, ignored
    • Any digits following non-digit characters will be discarded

Find out more about NaN (Not a Number) in our article here.

Examples – Converting to Floating-Point Numbers

Here are some examples of parseFloat() in action – the expected results are in the code comments:

parseFloat(4.25); // A floating number with value 4.25 - the number will be converted to a string and then to a float
parseFloat('4.25'); // A floating number with value 4.25 - the string will be converted to a float
parseFloat('  4.25  '); // A floating number with value 4.25 - the string will be converted to a float, ignoring whitespace
parseFloat('425e-3'); // A floating number with value 0.425 - the string is interpreted (including exponent e) and the value assigned to a float
parseFloat('4.25some these are not digits 123'); // A floating number with value of 4.25 - as there are interpretable digits prior to the non-digit characters, they are interpreted until the first non-digit character
parseFloat('OUCH342');// NaN as there are no interpretable digits prior to the first non-digit character

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment