2017-03-21 39 views
-1

我有以下代码段,它适用于以下货币格式:249.00但是我放置在使用欧元的网站上,价格是249,00的不同格式,但是当计算它疯了,使总计29.900,00JavaScript toFixed,toString替换货币失败

有谁知道我需要做什么变化,以便与新的货币变化?

var total = parseFloat(a) + parseFloat(b); 
total = parseFloat(Math.round(total * 100)/100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
total = total.replace(/\.|\,/g,''); // remove thousand , and decimal . 
total = Shopify.formatMoney(total, '{{ shop.money_format }}'); 
$('.cart-finalTotal span').html(total); 

全码:

$(document).ready(function() { 
    // Hide initial values that need updating 
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide(); 
    // get current delivery rate 
    $("#get_rate").trigger('click'); 
    // set a timeout and get total and shipping that was generated and add together for nnew total 
    setTimeout(function() { 
    // get cart sub-total 
    var a = $(".cart-total span").html().replace(/[^0-9.]/gi, ""); 

    if ($("#estimated-shipping em").text() == "FREE") { 
     var b = "0.00"; 
     var isFree = "true"; 
    } else { 
     var b = parseFloat($("#estimated-shipping em").text().replace(/[^0-9.]/gi, "")); 
    } 
    if(isFree == "true") { 
     $("#estimated-shipping em").html("{{ 'cart.shipping_estimator.free' | t }}"); 
    } else { 
     var shopifyShipping = b.toFixed(2); 
     shopifyShipping = shopifyShipping.replace(/\.|\,/g,''); // remove thousand , and decimal . 
     shopifyShipping = Shopify.formatMoney(shopifyShipping, '{{ shop.money_format }}'); 
     $("#estimated-shipping em").html(shopifyShipping); 
    } 

    // add together sub-total and estimated shipping to new total 
    // update with new total with sub-total and added shipping 
    var total = parseFloat(a) + parseFloat(b); 
    total = parseFloat(Math.round(total * 100)/100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
    total = total.replace(/\.|\,/g,''); // remove thousand , and decimal . 
    total = Shopify.formatMoney(total, '{{ shop.money_format }}'); 
    $('.cart-finalTotal span').html(total); 

    // show new values 
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show(); 

    //console.log('{{ 'cart.shipping_estimator.free' | t }}'); 
    }, 2000); 
    $(".item-quantity input").on("change", function() { 
    document.location.href = location.href 
    }); 
}); 
+1

你需要用逗号替换逗号,_before_你pa将该值赋给parseFloat。 – CBroe

+0

在这部分中逗号? .replace(/ \ B(?=(\ d {3})+(?!\ d))/ g,“。”);如果是这样我尝试了,但似乎没有解决这个问题。 – James

+0

不,在您尝试将值249,00分析为浮点的部分。 – CBroe

回答

0

我知道一个简单的解决方案,但对那里的任何通知,这可能帮助刚刚加入替换帮助

.replace(/[^0-9.]/gi, ""); 

.replace(/[^0-9.,]/gi, "");