javascript jQuery 计算 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4542012/
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-10-25 14:03:08  来源:igfitidea点击:

jQuery calculation NaN

javascriptjquerynan

提问by Lorenzo

I have a view that output decimal values that represent money data types on the database. To output I am using the following formatting code

我有一个输出代表数据库中货币数据类型的十进制值的视图。要输出我使用以下格式代码

string price = String.Format("{0:f}", (item.Price + item.VAT));

that produce a string like this

产生这样的字符串

12,99 (with comma as the decimal separator)

I am now using this string to make some calculation on the client using jQuery

我现在使用这个字符串在客户端上使用 jQuery 进行一些计算

elmProductSelected.children("option").each(function(n) {
    var pIdx = $(this).attr("rel"); 
    var pObj = products.eq(pIdx);
    var pValue = $(this).attr("value");
    var valueArray = pValue.split('|');
    var prdId = valueArray[0];
    var pQty = valueArray[1];
    /* the value of pPrice is 12,99 after the following call */
    var pPrice =  $(pObj).attr(attrProductPrice);
    /* I get NaN here! */
    sTotal = sTotal + ((pPrice - 0) * (pQty - 0));
    cProductCount++;
    cItemCount = cItemCount + (pQty-0);
});

I am getting NaNright on the line I have commented. I suppose that this is due to the fact that the pPricevalue use the comma as the decimal separator because if I manually set it as 12.99everything works.

我在我评论的那一行得到了NaN。我想这是因为该pPrice值使用逗号作为小数点分隔符,因为如果我手动将其设置为12.99一切正常。

Is there a way to read the 12,99as a number using javascript/jQuery?

有没有办法12,99使用 javascript/jQuery读取数字?

回答by Webnet

NaN = Not a number

NaN = 不是数字

By using parseIntyou can tell Javascript that it should be treated as a number.

通过使用parseInt您可以告诉 Javascript 它应该被视为一个数字。

var pPrice = parseInt($(pObj).attr(attrProductPrice));

You might need parseFloatif you're using a decimal.

parseFloat如果您使用的是小数,则可能需要。

回答by VoY

There are two problems with the code. First, you should cast the string to float, as Webnet suggests. The parseFloat function, however, will expect the argument to use dot as a decimal separator. Therefore, the following code will do what you want:

代码有两个问题。首先,您应该按照 Webnet 的建议将字符串转换为浮点数。然而, parseFloat 函数将期望参数使用点作为小数点分隔符。因此,以下代码将执行您想要的操作:

var pPrice = $(pObj).attr(attrProductPrice);
pPrice = parseFloat(pPrice.replace(',', '.'));

First, you replace the comma for a dot, then convert the result to a float.

首先,将逗号替换为点,然后将结果转换为浮点数。

回答by Frizi

try this

试试这个

var pPrice = $(pObj).attr(attrProductPrice).split(",").join(".");

回答by user113716

If that's the only comma separator there will be (none for a thousands separator), then just replace it.

如果那是唯一的逗号分隔符(千位分隔符没有),那么只需替换它。

var num = parseInt( str.replace( ',', '.' ) ); // or parseFloat

If there are other characters that will prevent correct parsing, then I'd suggest having a custom attribute that stores the undecorated number.

如果还有其他字符会阻止正确解析,那么我建议使用一个自定义属性来存储未修饰的数字。

<input data-undecorated="123.45" value="123,45" />
var num = parseInt( $('#myInput').data('undecorated') ); // or parseFloat

You can use data()like this to get data-attributes if you're using jQuery 1.4.3or later.

如果您正在使用或稍后使用,您可以data()像这样使用来获取data-属性jQuery 1.4.3

回答by Chandu

Try this RegEx:

试试这个正则表达式:

Update: Changed the expression, since I missed the part(with comma as the decimal separator).

更新:更改了表达式,因为我错过了部分(用逗号作为小数点分隔符)。

var pPrice = $(pObj).attr(attrProductPrice)..replace(/(,)(\d+)$/, ".").replace(",","")

回答by keegan3d

This looks related: How to convert string into float in javascript?

这看起来相关:How to convert string into float in javascript?

It looks like you'll have to replace the comma with a decimal.

看起来您必须用小数替换逗号。

parseFloat('12,99'.replace(',', '.'))

parseFloat('12,99'.replace(',', '.'))