将带有点或逗号的字符串作为小数分隔符转换为JavaScript中的数字 [英] Convert String with Dot or Comma as decimal separator to number in JavaScript

查看:134
本文介绍了将带有点或逗号的字符串作为小数分隔符转换为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(' ',''))

这篇关于将带有点或逗号的字符串作为小数分隔符转换为JavaScript中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆