node.js - NodeJS : Convert string X, XXX.XX float

标签 node.js string floating-point string-parsing

我打扰你了,因为我想用 node.JS 将字符串转换为 float 。 我有一个从请求返回的字符串,看起来像: x,xxx.xxxx(例如 4,530.1200),我想将其转换为 float :xxxx.xxxx(或 4530.1200)。 我试过使用: parseFloat(str) 但它返回 4。我还在网上找到了 Float.valueOf(str),但我收到了一个 ReferenceError: Float is not defined 错误。

最佳答案

您可以使用字符串替换来解决此类问题。

str = '4,530.1200';
parseFloat(str.replace(/,/g, ''));

这将从您的字符串中删除所有 ,,以便您可以使用 parseFloat 将其转换为 float 。

注意正则表达式中的g标志,它代表全局,通过使用它您指定删除所有匹配的正则表达式。

关于node.js - NodeJS : Convert string X, XXX.XX float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45772198/

相关文章:

r - 查找字符在字符串中的位置

string - 是否有Delphi XE2字符串哈希函数保证可以用于查找的唯一性?

floating-point - 了解这些 float 如何工作?

javascript - 在接受用户的答案之前,如何将我的问题显示到控制台?

c# - 从大 float 中删除 E 符号,C#?

c++ - 如何在 C++ 中将字符串转换为具有 6 位精度的 double ?

node.js - CouchDB 中的部分搜索/查找

node.js - 如何在nodejs cassandra驱动程序中设置查询的键空间?

node.js - 重定向 http ://to https://in node. js 最有效和性能最高的方法是什么 [概念观点。]

javascript - 如何查找进入 Node.js 构造函数的参数数量