我有一个 View ,可以输出代表数据库上货币数据类型的十进制值。为了输出,我使用以下格式化代码
string price = String.Format("{0:f}", (item.Price + item.VAT));
产生这样的字符串
12,99 (with comma as the decimal separator)
我现在使用这个字符串在客户端使用 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
一切正常。
有没有办法使用 javascript/jQuery 将 12,99
读取为数字?
最佳答案
NaN = 不是数字
通过使用parseInt
,你可以告诉Javascript它应该被视为一个数字。
var pPrice = parseInt($(pObj).attr(attrProductPrice));
如果您使用小数,则可能需要 parseFloat
。
关于javascript - jQuery 计算 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4542012/