Use toFixed() with parseFloat to get output numbers up to 2 decimals in JavaScript.
parseFloat(yourString).toFixed(2)
If you need performance (like in games), use the math round method.
Math.round(number * 100) / 100
It’s about 100 times as fast as parseFloat(number.toFixed(2))
JavaScript parseFloat 2 decimals
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
var str = "100";
var twoPlacedFloat = parseFloat(str).toFixed(2);
console.log(twoPlacedFloat);
</script>
</body>
</html>
Output:
Do comment if you have any doubts or suggestions on this JS parseFloat topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version