将带有点或逗号的字符串作为小数分隔符转换为JavaScript中的数字
问题描述:
输入元素包含数字a,其中逗号或点用作小数分隔符,空格可用于对数千个进行分组:
An input element contains numbers a where comma or dot is used as decimal separator and space may be used to group thousands like this:
'1,2'
'110 000,23'
'100 1.23'
'1,2'
'110 000,23'
'100 1.23'
如何使用JavaScript将它们转换为浏览器中的浮点数?
How would one convert them to a float number in the browser using JavaScript?
使用jQuery和jQuery UI。 Number(字符串)
返回 NaN 和 parseFloat()
在第一个空格或逗号上停止。
jQuery and jQuery UI are used. Number(string)
returns NaN and parseFloat()
stops on first space or comma.
答
首先替换:
parseFloat(str.replace(',','.').replace(' ',''))