2017-03-21 37 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, "");