如何在 JavaScript 中将字符串转换为浮点数?
问题描述
我正在尝试解析数据网格中的两个值.这些字段是数字,当它们有逗号时(例如 554,20),我无法获取逗号后的数字.我试过 parseInt
和 parseFloat
.我该怎么做?
I am trying to parse two values from a datagrid. The fields are numeric, and when they have a comma (ex. 554,20), I can’t get the numbers after the comma. I’ve tried parseInt
and parseFloat
. How can I do this?
推荐答案
如果它们是单独的值,试试这个:
If they’re meant to be separate values, try this:
var values = "554,20".split(",")
var v1 = parseFloat(values[0])
var v2 = parseFloat(values[1])
如果它们是一个单一的值(比如在法语中,一半写成 0,5)
If they’re meant to be a single value (like in French, where one-half is written 0,5)
var value = parseFloat("554,20".replace(",", "."));
这篇关于如何在 JavaScript 中将字符串转换为浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2
如何在 JavaScript 中将字符串转换为浮点数? 为WP2原创文章,链接:https://www.wp2.cn/other/%e5%a6%82%e4%bd%95%e5%9c%a8-javascript-%e4%b8%ad%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%b5%ae%e7%82%b9%e6%95%b0/