如何使用逗号千位分隔符将字符串解析为数字? [英] How can I parse a string with a comma thousand separator to a number?

查看:100
本文介绍了如何使用逗号千位分隔符将字符串解析为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2,299.00 作为字符串,我正在尝试将其解析为一个数字。我尝试使用 parseFloat ,结果为2.我猜这个逗号是问题,但我怎么能以正确的方式解决这个问题呢?只需删除逗号?

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, but how would I solve this issue the right way? Just remove the comma?

var x = parseFloat("2,299.00")
alert(x);

推荐答案

是删除逗号:

parseFloat(yournumber.replace(/,/g, ''));

这篇关于如何使用逗号千位分隔符将字符串解析为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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