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

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

get numeric value from string jquery

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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, ''));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...