如何将字符串转换为数字? [英] How to convert string to number?

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

问题描述

如何将 "4,250,000.40" 转换为 4,250,000.40,即通过保留逗号和点将字符串转换为数字?使用 JavaScript

How to convert "4,250,000.40" to 4,250,000.40 that is converting string to number by remaining the commas and dots? using JavaScript

推荐答案

您可以使用 parseFloat(str) 将字符串转换为数字,但首先您需要删除字符串中的逗号,因为 parseFloat 不起作用对于包含逗号的数字.

You can use parseFloat(str) to convert a string to a number, but first you need to remove the commas from the string, as parseFloat doesn't work for numbers with commas in them.

parseFloat(str.replace(/,/g, ""));

这篇关于如何将字符串转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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