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 8625299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:45:00+00:00 2026-06-12T07:45:00+00:00

in my webpage I have a total in currency format that can either be

  • 0

in my webpage I have a total in currency format that can either be positive or negative.

Example $5.50 or $(5.50). 

This value is nothing other than text contained within a span tag. I’m trying to read the value and convert it into a numeric value in js where I can then perform math calculations against it.

Example $5.50 -> 5.50 and $(5.50) -> -5.50

I have written the following regex script to handle converting negative currency values into numeric values

var regex = /^\$*?\((\d+(\.)?(\d+)?)\)$/

I have the following methods to handle retrieving and converting the value.

//retrieve value from template
$.fn.fieldVal = function () {
    var val;

    if ($(this).is(':input')) {
        val = $(this).val();
    } else {
        val = $(this).text();
    }    

    return convertCurrencyToNumeric(val);

};

//convert currency to numeric value
function convertCurrencyToNumeric(n) {
    var regex = /^\$*?\((\d+(\.)?(\d+)?)\)$/

    n = n.replace(/[^0-9-\.]/g, '');

    if(isNumber(n)) {
        n = parseFloat(n);
        return n;
    }
    return 0;
}

//test if numeric
function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

I’m not clear how to first test if the value is negative and secondly if negative, replace the value with regex outcome.

  • 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-06-12T07:45:01+00:00Added an answer on June 12, 2026 at 7:45 am

    Note: Negating classes can really clear up regEx problems, IMO. And no, I don’t care what JSLint’s opinion on the matter is. Using ‘.’ is slow and clumsy and the rationale given for that particular lint gotcha is absurd.

    function convertCurrency(str){
        var negMatch = ( str.match(/(^\$-|^-\$|^$\()/g) ), //handles -$5.05 or $-5.05 too
        str = str.replace(/[^\d.]/g,''), //anything that's not a digit or decimal point
    //gotcha, Europeans use ',' as a decimal point so there's a localization concern
        num = parseFloat(str);
    
        if(negMatch){ num *= -1; }
    
        return num;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement that in a webpage I have multiple block of content
At my webpage I have links to images like this: img/123456765/mypicture.jpg (<img src=img/123456765/mypicture.jpg />)
I have a webpage that is transferring A stylesheet as a text/plain MIME type.
I have a webpage that when landed on, will detect a user's physical location
I have a webpage that retrieves data (via ajax/php) and shows it in an
I have data grids on my webpage that need to be refreshed every few
I have around 50 textboxes in my webpage, where user can enter numeric values.
I have been at this quiz for ages now and just can't get it
I'd like to create a webpage which looks like this: https://i.stack.imgur.com/GYEis.png I can't use
I have a Javascript function on my webpage that I'm displaying in a UIWebView:

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.