jQuery计算NaN [英] jQuery calculation NaN

查看:282
本文介绍了jQuery计算NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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));

产生这样的字符串

12,99 (with comma as the decimal separator)

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

I am now using this string to make some calculation on the client using 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);
});

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

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

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

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

推荐答案

NaN =不是数字

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

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

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

你可能需要 parseFloat 如果你是使用小数。

You might need parseFloat if you're using a decimal.

这篇关于jQuery计算NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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