You can use toFixed() to do that
var twoPlacedFloat = parseFloat(yourString).toFixed(2)
Answer from Mahesh Velaga on Stack Overflow Top answer 1 of 16
1146
You can use toFixed() to do that
var twoPlacedFloat = parseFloat(yourString).toFixed(2)
2 of 16
92
If you need performance (like in games):
Math.round(number * 100) / 100
It's about 100 times as fast as parseFloat(number.toFixed(2))
http://jsperf.com/parsefloat-tofixed-vs-math-round
EDIT:
https://jsperf.app/kowulu
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-parse-float-with-two-decimal-places-in-javascript
How to Parse Float with Two Decimal Places in JavaScript? - GeeksforGeeks
Below are the approaches to parse ... the string isn't numeric, it returns NaN. To limit the number to two decimal places, use toFixed(2), which rounds the result....
Published July 23, 2025
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript | MDN
The parseFloat() function parses a string argument and returns a floating point number. function circumference(r) { return parseFloat(r) * 2.0 * Math.PI; } console.log(circumference(4.567)); // Expected output: 28.695307297889173 console.log(circumference("4.567abcdefgh")); // Expected output: ...
EyeHunts
tutorial.eyehunts.com › home › javascript string to float 2 decimal | example code
JavaScript string to float 2 decimal | Example code
June 29, 2022 - Simple example code parse float with two decimal places. When you use toFixed, it always returns the value as a string. This sometimes complicates the code. <!doctype html> <head> <script> var str = "100.999"; var res = parseFloat(str).toFixed(2) console.log(res); </script> </head> <body> </body> </html>
Three.js
discourse.threejs.org › discussion
🙊 Change floating-point to 2 decimal places? - Discussion - three.js forum
Before I go down this rabbit hole further. If you could share your wisdom/experience. I’m assuming with certain large floating-point numbers is a known problem and CAN NOT be converted to 2 decimal places. As these large numbers appear to be an error, it is the way things work with my current ...
Published July 24, 2018
IncludeHelp
includehelp.com › code-snippets › how-to-parse-float-with-two-decimal-places-in-javascript.aspx
How to parse float with two decimal places in JavaScript?
// Float value as string const str = "123.456"; // Parsing to float with two decimal digits var result = parseFloat(str).toFixed(2); // Printing result console.log("Float with two decimal digits: ", result);
TutorialsPoint
tutorialspoint.com › how-to-parse-float-with-two-decimal-places-in-javascript
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 ac
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toFixed
Number.prototype.toFixed() - JavaScript | MDN
The toFixed() method of Number values returns a string representing this number using fixed-point notation with the specified number of decimal places. function financial(x) { return Number.parseFloat(x).toFixed(2); } console.log(financial(123.456)); // Expected output: "123.46" console.lo...
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-parse-float-with-two-decimal-places-in-typescript
How to Parse Float with Two Decimal Places in TypeScript ? - GeeksforGeeks
July 23, 2025 - Example: The below example uses the Number constructor and toFixed method to convert the number 123.45678 into a string with two decimal places and then parses it back to a floating-point number (123.46).
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
The parseFloat() method parses a value as a string and returns the first number.
Top answer 1 of 15
18
var roundUpto = function(number, upto){
return Number(number.toFixed(upto));
}
roundUpto(0.1464676, 2);
toFixed(2): Here 2 is the number of digits up to which we want to round this number.
2 of 15
18
2022, native, without library, modern browser, clear and readable.
function round(
value,
minimumFractionDigits,
maximumFractionDigits
) {
const formattedValue = value.toLocaleString('en', {
useGrouping: false,
minimumFractionDigits,
maximumFractionDigits
})
return Number(formattedValue)
}
console.log(round(21.891, 2, 3)) // 21.891
console.log(round(1.8, 2)) // 1.8, if you need 1.80, remove the `Number` function. Return directly the `formattedValue`.
console.log(round(21.0001, 0, 1)) // 21
console.log(round(0.875, 3)) // 0.875
Stack Overflow
stackoverflow.com › questions › 27061322 › convert-string-to-float-up-to-2-decimal-places
javascript - Convert String to Float up to 2 Decimal places - Stack Overflow
then use var float = parseFloat(newFloat); (because toFixed() returns string) to convert to float again. ... and than you end up with the variable float being this: 1.1 instead of 1.10. You are still wrong 2014-11-21T12:44:14.13Z+00:00
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
JavaScript toFixed() Method
❮ Previous JavaScript Number Reference Next ❯ · let num = 5.56789; let n = num.toFixed(); Try it Yourself » · let num = 5.56789; let n = num.toFixed(2); Try it Yourself » · More examples below · The toFixed() method converts a number ...
Flexiple
flexiple.com › javascript › parsefloat-javascript
parseFloat JavaScript: Syntax and Examples - Flexiple
Return value: It returns a floating-point Number and if the first character of a string cannot be converted to a number then the function returns NaN i.e, >not a number. function myFunction() { parseFloat("9") Output: 9 parseFloat("11.00") Output: 9 parseFloat("10.89") Output: 10.89 parseFloat("10 20 30") Output: 10 parseFloat(" 100 ") Output: 100 parseFloat("1 xyz") Output: 1 parseFloat("xyz 1") Output: NaN } If parseFloat encounters a character other than a plus(+), minus (-), numerals (0–9), decimal (.), or exponent (e or E), it returns the value up to that character, ignoring the invalid character and characters following it.
Top answer 1 of 9
445
If they're meant to be separate values, try this:
var values = "554,20".split(",")
var v1 = parseFloat(values[0])
var v2 = parseFloat(values[1])
If they're meant to be a single value (like in French, where one-half is written 0,5)
var value = parseFloat("554,20".replace(",", "."));
2 of 9
64
Have you ever tried to do this? :p
var str = '3.8';ie
alert( +(str) + 0.2 );
+(string) will cast string into float.
Handy!
So in order to solve your problem, you can do something like this:
var floatValue = +(str.replace(/,/,'.'));