当我使用parseFloat时,不要理解发生了什么

时间:2018-06-07 11:50:48

标签: javascript

所以基本上我有一个系统,显示你经常一起买的物品。但是,我需要显示整个捆绑的值,基本上是项目1 +项目2 +项目3。 所以,我试图做的是采用附加价格的范围,在其上使用$.text()方法然后对所有数字求和,但是,我似乎无法得到它们,当我使用parseFloat()时,它会返回NaN 我的getPrice函数如下所示:

function getPrice(discount) {   
        var total_price = 0;
        var active_products = 0;
        $('.bundle-product-checkbox').each(function() {
            if($(this).prop('checked') == true) {
                var price = parseFloat($('.bundle-fbt-price > .money').text());
                total_price += price;
                active_products++;
                console.log(total_price);                       
            }
    });

if (active_products == $('.bundle-product-checkbox').length) {
    var savings = (discount / 100) * total_price;
    var discountedPrice = total_price - (discount / 100) * total_price;
    $('.full-bundle-price').text(total_price + ' $').css("text-decoration", "line-through");
    $('.discounted-bundle-price').text(discountedPrice + ' $');
    $('.bundle-savings').text('You save ' + savings + ' $');
} else if (active_products == 0) {
    $('.bundle-add').fadeOut('fast');
} else {
    $('.full-bundle-price').text('Total Price: ' + total_price + ' $');
    $('.full-bundle-price').css("text-decoration", "none");
    $('.discounted-bundle-price').text('');
    $('.bundle-savings').text('');
}

这部分的html是

{foreach from=$products item=p key=k}
<div style="display:inline-block;float:left;width:100%;">
    <input type="checkbox" checked="checked" class="bundle-product-checkbox" name="bundle-product-list" data-toggle="{$k}" />
    <span class="bundle-title">{if $k==0 }<b>This Item: {/if}{$p.title} {if $k==0 }</b>{/if}</span> 
                    {if isset($p.variants)}
                        {if $p.variants_count > 1}
                            <select class="vitals_recommended_select bundle_fbt_select">
                                {foreach $p.variants item=v}
                                    <option value="{$v.id}" class="bundle-fbt-money" data-vitals-price="{if $display_currency_code}{$shop.money_with_currency_format|replace:array('{{amount}}','{{amount_with_comma_separator}}'):$v.price}{else}{$shop.money_format|replace:array('{{amount}}','{{amount_with_comma_separator}}'):$v.price}{/if}" data-vitals-discount="{if $v.discount}-{$v.discount}%{else}0{/if}">{$v.title}</option>
                                {/foreach}
                            </select>
                            <p class="vitals-product-price bundle-fbt-price">{if $display_currency_code}{$shop.money_with_currency_format|replace:array('{{amount}}','{{amount_with_comma_separator}}'):$p.variants[0].price}{else}{$shop.money_format|replace:array('{{amount}}','{{amount_with_comma_separator}}'):$p.variants[0].price}{/if} {if $p.variants[0].discount} <span class="vitals-product-discount">-{$p.variants[0].discount}%</span>{/if}</p>
                        {else}
                            <p class="vitals-product-price bundle-fbt-price">{if $display_currency_code}{$shop.money_with_currency_format|replace:array('{{amount}}','{{amount_with_comma_separator}}'):$p.variants[0].price}{else}{$shop.money_format|replace:array('{{amount}}','{{amount_with_comma_separator}}'):$p.variants[0].price}{/if} {if $p.variants[0].discount}<span class="vitals-product-discount">-{$p.variants[0].discount}%</span>{/if}</p>
                        {/if}
                    {/if}
</div>
{/foreach}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

  

当我使用parseFloat时,不明白发生了什么?

所有正在发生的事情是,您尝试解析的值不是parsebale,因为它们以字母字符开头并包含字母字符,因此无法解析它们并返回NaN

如果您要解析的值是这样的:

VM598:1 $91.00 USD$45.00 USD$229.00 USD

您需要对其进行处理以便对其进行解析,您可以使用splitregexp从中提取alll值。

这是你可以做的:

var content = "VM598:1 $91.00 USD$45.00 USD$229.00 USD";

var values = content.split(":")[1].split(/\$|USD/).filter((v, i) => {
  return v !== "" && i > 0;
}).map(v => v.trim());
console.log(values);

var amounts = values.map(x => parseFloat(x));
console.log(amounts);