Javascript toFixed,toString替换货币失败

时间:2017-03-21 15:15:32

标签: javascript html

我有以下代码片段,它适用于以下货币格式:249.00但是我放置在使用欧元的网站上,而且定价的格式是249,00,但是在计算它时会变得疯狂并且制作总计29.900,00

有没有人知道我需要做出哪些改变才能使用新的货币变化?

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 个答案:

答案 0 :(得分:0)

我知道一个简单的解决方案,但有任何通知,这可能有助于添加替换帮助

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

{{1}}