How to parse float with two decimal places in JavaScript?


To parse float with two decimal places, use the concept of toFixed(2). Following is the code −

Example

var price = 178932.89;
var tax = 7.9;
var totalPrice = price*tax;
console.log("The actual value="+totalPrice);
var gettingTwoPlacedValues = parseFloat(totalPrice).toFixed(2);
console.log("After getting two placed values="+gettingTwoPlacedValues);

To run the above program, you need to use the following command −

node fileName.js.

Output

Here, my file name is demo142.js. This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo142.js
The actual value=1413569.8310000002
After getting two placed values=1413569.83

Updated on: 11-Sep-2020

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements