javascript parseFloat '500,000' returns 500 when I need 500000

前端 未结 7 1504
清酒与你
清酒与你 2020-11-30 05:52

How would it be a nice way of handling this?

I already thought on removing the comma and then parsing to float.

Do you know a better/cleaner way?

Th

7条回答
  •  抹茶落季
    2020-11-30 06:16

    You can use the string replace method, but not in a one liner as a regexp allows.

    while(str.indexOf(',')!=-1)str= str.replace(',','');
    parseFloat(str);
    

    Or to make a single expression without a regexp=

    return parseFloat(str.split(',').join(''));
    

    I'd use the regexp.

提交回复
热议问题