How can I parse a string with a comma thousand separator to a number?

后端 未结 16 2834
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 10:18

I have 2,299.00 as a string and I am trying to parse it to a number. I tried using parseFloat, which results in 2. I guess the comma is the problem

16条回答
  •  天涯浪人
    2020-11-22 10:24

    Number("2,299.00".split(',').join(''));   // 2299
    
    

    The split function splits the string into an array using "," as a separator and returns an array.
    The join function joins the elements of the array returned from the split function.
    The Number() function converts the joined string to a number.

提交回复
热议问题