How to convert string into float in JavaScript?

2019-01-04 00:16发布

I am trying to parse two values from a datagrid.
The fields are numeric, and when they have a comma (ex. 554,20), I can't get the numbers after the comma.
I've tried parseInt and parseFloat. How can I do this?

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-04 01:05

If you extend String object like this..

String.prototype.float = function() { 
  return parseFloat(this.replace(',', '.')); 
}

.. you can run it like this

"554,20".float()
> 554.20

works with dot as well

"554.20".float()
> 554.20

typeof "554,20".float()
> "number"
查看更多
登录 后发表回答