Javascript jQuery + parseDouble 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2063048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:37:42  来源:igfitidea点击:

jQuery + parseDouble problem

javascriptjqueryparsingdouble

提问by Stefan Konno

I have an AJAX-request that returns a json object containing a few values with two decimals each, but since it's json these values are strings when returned. What I need to do is to perform addition on these values. Just a simple a+b = c, but they concatenate instead becoming ab.

我有一个 AJAX 请求,它返回一个 json 对象,其中包含几个值,每个值都有两位小数,但由于它是 json,因此这些值在返回时是字符串。我需要做的是对这些值执行加法。只是一个简单的 a+b = c,但它们连接起来变成了 ab。

I was hoping I could use parseDouble in jQuery just like I can use parseInt but apparantly I can't. At least not what I've found. So the question remains, is there any way I can add these two string values into a double or float value? Or should I just calculate this on the server side and send the already additioned value back to the browser and jQuery.

我希望我可以在 jQuery 中使用 parseDouble,就像我可以使用 parseInt 一样,但显然我不能。至少不是我发现的。所以问题仍然存在,有什么方法可以将这两个字符串值添加到双精度或浮点值中?或者我应该在服务器端计算这个并将已经添加的值发送回浏览器和 jQuery。

Example:

例子:

This is what happens 5.60 + 2.20 = 5.602.20

这就是发生的事情 5.60 + 2.20 = 5.602.20

This is what should happen 5.60 + 2.20 = 7.80

这就是应该发生的 5.60 + 2.20 = 7.80

Thankful for answers.

感谢您的回答。

回答by cletus

Just use parseFloat():

只需使用parseFloat()

var c = parseFloat(a) + parseFloat(b);