Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6694011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:03:09+00:00 2026-05-26T06:03:09+00:00

I have retrieved a string and I extracted a decimal number from it using

  • 0

I have retrieved a string and I extracted a decimal number from it using regex, e.g. 1.00 or 3.99. Now I wanna use these numbers for calculation. How can I convert those regex results into proper numbers so I can perform calculation? In the code below, every click should add the price to the previous total but it doesn’t do the sum of numbers properly.

var SaveAfrica = {
  init: function () {
    this.addToBasket();
  },

  addToBasket: function () {
    var featured = $('section.featured'),
        bsktTotalSpan = $('.basket_total span.basket-qty'),
        list = featured.find('ul'),
        item = list.find('li');

    featured.delegate('.buy-now', 'click', function (e) {
        var thisElem = $(this), 
            qtyData = thisElem.closest('li').data('price'),
            total = parseInt(bsktTotalSpan.text()) + parseFloat(qtyData.match(/[\d\.\d]+/i));


        e.preventDefault();

        bsktTotalSpan.text(parseFloat(total));

        console.log('The total is: total);

    });
  }
};

SaveAfrica.init();

The HTML where the digit is from:

<li data-price="&pound;2.99"><a href="#" class="buy-now"><img src="someImage.jpg" alt="some image" /></a></li>

Many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:03:09+00:00Added an answer on May 26, 2026 at 6:03 am

    What does the sum come out to, if anything at all?

    You may want to try the following instead:

    total = parseFloat(bsktTotalSpan.text()) + parseFloat(qtyData.match(/[\d\.\d]+/i));
    

    That is, parse both of them to floats And explicitly define total as a float. Otherwise, the decimals might be getting truncated somewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So you have a String that is retrieved from an admin web UI (so
Is it possible to extract a number from a string. For example, I have
I have two entity lists being retrieved from the database they are using the
I have a coded string that I'd like to retrieve a value from. I
Let's say I have a string that I retrieve from a DB like: Lorem
I have a json string saved in my db. When i retrieve it from
I have written some code to look at properties using reflection. I have retrieved
I have this sample text, which is retrieved from the class name on an
I am trying to get the contact's phone number after I have retrieved their
I am trying to extract values from a string, I have tried to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.