8

Im using the parseFloat method to convert a string to a float. it works fine however when the number exceed thousand it one return the value in the thousand place.

So parseFloat('1,022.55') returns 1 instead of 1022.55 How do i solve this?

MarsOne
  • 2,075
  • 4
  • 26
  • 50
  • Does this answer your question? [How can I parse a string with a comma thousand separator to a number?](https://stackoverflow.com/questions/11665884/how-can-i-parse-a-string-with-a-comma-thousand-separator-to-a-number) – Heretic Monkey Aug 05 '20 at 13:22

2 Answers2

19

Try:

parseFloat('1,022.55'.replace(/,/g, ''))
jcubic
  • 56,835
  • 46
  • 206
  • 357
2

Here it is annotated

originalNum = '1,022.55';
cleanNum = originalNum.replace(",", "");
float = parseFloat(cleanNum);
console.log(float);

Alternatively you can just make it a one-liner by using

float = parseFloat('1,022.55'.replace(",", ""));
console.log(float);