In this article, I will show you how to parse Float with 2 Decimal places in JavaScript using two standard methods that are the toFixed()
method and the Math.round()
function.
Parse Float with 2 Decimal places in JavaScript.
Using the toFixed() method
The toFixed()
method in JavaScript is a method that is used to convert a number value to a string value and round the number to a decimal number with a specified number of decimal places.
To parse Float with 2 decimal places. Firstly, we need to use the parseFloat()
function. This function will parse the input string and convert it to a number. Second, we call the toFixed()
method to round the input variable to the specified decimal place.
Syntax:
parseFloat(myString).toFixed(2)
The parseFloat(myString)
will convert myString
to the number, and then toFixed(2)
will round it to two decimal places
For instance:
let myString = 2.71828; let myFloat = parseFloat(myString).toFixed(2); console.log(myString + " after being parsed float with 2 decimal places is " + myFloat); console.log("Type of myFloat is " + typeof myFloat);
Output:
2.71828 after being parsed Float with 2 decimal places is 2.72
Type of myFloat is string
By using toFixed()
, we will receive a string, so if you want to receive a number instead of a string, you can use parseFloat()
again.
Using the Math.round() function
The Math.round()
function in JavaScript is a function of a math object. It returns the nearest integer rounded from the specified number.
Syntax:
Math.round(number)
For instance:
let myString = 2.71828; let myFloat = Math.round(myString * 100) / 100; console.log(myString + " after being parsed float with 2 decimal places is " + myFloat); console.log("Type of myFloat is " + typeof myFloat);
Output:
2.71828 after being parsed Float with 2 decimal places is 2.72
Type of myFloat is number
The Math.round( myString * 100) / 100
is used to guarantee the place of the decimal. Which ensures that we will get two digits after the comma after rounding.
This way will return a numeric value, but if there is a trailing zero after the decimal point, it will be removed. For example, ‘1.10’ will become ‘1.1’.
Summary
Through this article, we have learned how to parse Float with 2 Decimal places in JavaScript using two methods. Depending on your purpose, you can choose one of the above two ways. If you want the return value to be a string, you can use the toFixed()
method. If the value you want to get back is a numeric type, choose the Math.round()
function. I hope you enjoy this article.
Maybe you are interested:
- How to get a Random Float in a Range using JavaScript
- Round a Number to 2 Decimal Places in JavaScript
- Round a Number to 3 Decimal Places in JavaScript

Hi, I’m Mary Ralston. I have experience programming in HTML, javascript, C++,… and I want to share with you my experience. So if you want to know more about the above languages read my articles