Using Number parseFloat() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the Number technique known as parseFloat() with syntax and examples.

Description

In JavaScript, parseFloat() is a Number technique that is used to parse a string and return its fee as a floating factor number. Because parseFloat() is a method of the Number object, it should be invoked thru the object referred to as Number.

Syntax

In JavaScript, the syntax for the parseFloat() technique is:

Number.parseFloat(string_value);

Parameters or Arguments

string_value The string cost to convert to a floating point number.

Returns

The parseFloat() technique parses a string and returns its fee as a floating factor number.

If the parseFloat() technique is surpassed a value that can not be converted to a floating point number, it will return NaN.

Example

Let’s take a appear at an instance of how to use the parseFloat() approach in JavaScript.

For example:

console.log(Number.parseFloat('25.678'));
console.log(Number.parseFloat('123ABC4'));
console.log(Number.parseFloat('ABC123'));

In this example, we have invoked the parseFloat() technique the use of the Number class.

We have written the output of the parseFloat() technique to the web browser console log, for demonstration purposes, to show what the parseFloat() approach returns.

The following will be output to the internet browser console log:

25.678
123
NaN

In this example, the first output to the console log again 25.678 which is the floating point quantity illustration of the string cost ‘25.678’.

The 2nd output to the console log again 123 considering that the parseFloat() approach will parse the string value ‘123ABC4’ until a non-numeric persona is encountered and then it will discard the the rest of the string.

The 0.33 output to the console log lower back NaN since the string cost ‘ABC123’ does now not start with a numeric value.