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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:07:58+00:00 2026-05-26T21:07:58+00:00

I have strings which contains thousand separators, however no string-to-number function wants to consume

  • 0

I have strings which contains thousand separators, however no string-to-number function wants to consume it correctly (using JavaScript). I’m thinking about “preparing” the string by stripping all thousand separators, leaving anything else untoched and letting Number/parseInt/parseFloat functions (I’m satisfied with their behavious otherwise) to decide the rest. But it seems what i have no idea which RegExp can do that!

Better ideas are welcome too!


UPDATE:

Sorry, answers enlightened me how badly formulated question it is. What i’m triyng to achieve is: 1) to strip thousand separators only if any, but 2) to not disturb original string much so i will get NaNs in the cases of invalid numerals.

MORE UPDATE:

JavaScript is limited to English locale for parsing, so lets assume thousand separator is ‘,’ for simplicity (naturally, it never matches decimal separator in any locale, so changing to any other locale should not pose a problem)

Now, on parsing functions:

parseFloat('1023.95BARGAIN BYTES!')  // parseXXX functions just "gives up" on invalid chars and returns 1023.95
Number('1023.95BARGAIN BYTES!')      // while Number constructor behaves "strictly" and will return NaN

Sometimes I use rhw loose one, sometimes strict. I want to figure out the best approach for preparing string for both functions.

On validity of numerals:

'1,023.99' is perfectly well-formed English number, and stripping all commas will lead to correct result.
'1,0,2,3.99' is broken, however generic comma stripping will give '1023.99' which is unlikely to be a correct result.

  • 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-26T21:07:59+00:00Added an answer on May 26, 2026 at 9:07 pm

    welp, I’ll venture to throw my suggestion into the pot:

    Note: Revised

    stringWithNumbers = stringwithNumbers.replace(/(\d+),(?=\d{3}(\D|$))/g, "$1");
    

    should turn

    1,234,567.12
    1,023.99
    1,0,2,3.99
    the dang thing costs $1,205!!
    95,5,0,432
    12345,0000
    1,2345
    

    into:

    1234567.12
    1023.99
    1,0,2,3.99
    the dang thing costs $1205!!
    95,5,0432
    12345,0000
    1,2345
    

    I hope that’s useful!

    EDIT:

    There is an additional alteration that may be necessary, but is not without side effects:

    (\b\d{1,3}),(?=\d{3}(\D|$))
    

    This changes the “one or more” quantifier (+) for the first set of digits into a “one to three” quantifier ({1,3}) and adds a “word-boundary” assertion before it. It will prevent replacements like 1234,123 ==> 1234123. However, it will also prevent a replacement that might be desired (if it is preceded by a letter or underscore), such as A123,789 or _1,555 (which will remain unchanged).

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

Sidebar

Related Questions

We have a Java project which contains a large number of English-language strings for
I have to write a library which contains a function that takes two strings
I have a String which contains XML nodes within it, and am seeking to
I have a string which contains binary data ( non-text data ). How do
I have a string which contains only numbers. Now I want to remove all
I have a number of strings which contain words which are bunched together and
I have somebody elses code (C# ASP.Net) which contains the following query: string query
So, I have a file called 'dummy' which contains the string: There is 100%
The variable filepath which is a string contains the value Música . I have
I have an ArrayList called account which contains Strings. I'm trying to write a

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.