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

  • Home
  • SEARCH
  • 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 8916677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:18:05+00:00 2026-06-15T05:18:05+00:00

I have spent time looking through each of the proposed solutions to the problem

  • 0

I have spent time looking through each of the proposed solutions to the problem of adjusting user input strings as they type to format a large number to have comma separators:

9999999999 –> 9,999,999,999 and so on.

There are several discussions here on stack overflow:

How to print a number with commas as thousands separators in JavaScript

Adding comma as thousands separator (javascript) – output being deleted instead

and many more related queries, I’ve tested those that I could find, and they worked well for numbers up to a power of 4 or 5, eg 12,345 comes out nicely,

But larger numbers end up with far too many commas:

1,,,,,0,,,0,,,000

for example is the output of the code I currently have implemented. (It also registers backspace as a key stroke, so when I backspace three times it adds a comma…)

function numberWithCommas(n){
    var s=n.split('.')[1];
    (s) ? s="."+s : s="";
    n=n.split('.')[0]
    while(n.length>3){
        s=","+n.substr(n.length-3,3)+s;
        n=n.substr(0,n.length-3)
    }
    return n+s
}

and then on the input:

onkeyup="this.value=numberWithCommas(this.value);"

Am I using the function incorrectly? Or is it simply not viable for larger numbers. Is there a way it can be adjusted to work for any size of number?

Thanks in advance

  • 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-15T05:18:07+00:00Added an answer on June 15, 2026 at 5:18 am

    You are just adding comas to a value which already as commas after the first iteration… You should just reset the commas at the beginning of your function numberWithCommas:

    n = n.replace(/,/g, "");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent much time looking for a special package that could run the
I have spent way too much time looking for the files owner icon to
I have spent some time now to solve a problem for which i have
I have spent quite a lot of time reading through AngularJS documentation and several
I am currently making an Android launcher and have spent a lot of time
I have to get the sub total of time spent in SQL any ideas?
I have struggled a lot spent quiet a lot of time but unable to
I've spent a lot of time working in Django, and have grokked the framework
I have little understanding of HTACCESS, even though I have spent a long time
I have been looking through Adobe's File Format Specification for PSD (Photoshop files) and

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.