Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am showing prices in span class want to add all the prices.To do that I have to remove the currency(which is dynamic).

data will be

<div class="js-price-cell">
  <span class="js-price">$280.00</span>
</div>
<div class="js-price-cell">
  <span class="js-price">$280.00</span>
</div>

I tried with

$form.find('.js-price-cell').each(function () {
  let priceText= $(this).find('.js-price').text();
  var itemTotal= parseFloat(priceText.replace(/[^0-9]/gi, ''));
  totalAmt += itemTotal;
});

Instead of 560.00. It is giving as 56000. Seems like even the . is getting replaced.

question from:https://stackoverflow.com/questions/65941059/get-numeric-value-from-string-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

You replaced the priceText: all non-decimals are removed. Add a . to the regex and it should work.

var itemTotal= parseFloat(priceText.replace(/[^0-9.]/gi, ''));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...