Parse Float with 2 Decimals

In this article, I am going to show you how to print parse float with 2 decimals in javascript. To print parsfloat() value with 2 decimals, we need to use the “toFixed()” method after using the parseFloat() method.

parsFloat(value)

It converts a given value to a float number, if not possible to convert then it returns NaN.

toFixed(value)

It is used to return the given value to a fixed value. For example, If we give a float number “3.2342345” and set the toFixed() method to 2, it will return the first 2 decimal numbers like “3.23”.

Example code:

var a= parseFloat("12.345345");
var b=parseFloat("12.345345").toFixed(2);
document.write("Normal ParseFloat Method <br/>")
document.write(a, "<br/>");
document.write("Normal ParseFloat Method with 2 decimal <br/>")
document.write(b);

Output:

Steps:

1. Here, we have created variable “a” and initialized value to String “12.345345”. So it will return the whole float value.
2. Then we created variable “b” and initialized the value to string “12.345345” then we use the toFixed(2) method. So it will return the first 2 decimals.