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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:14:02+00:00 2026-05-25T10:14:02+00:00

I came up with this, since other examples provided on stackoverflow were in C#

  • 0

I came up with this, since other examples provided on stackoverflow were in C#

string number_fmt(ulong n)
{
    // cout << "(" << n << ")" << endl;
    char s[128];
    sprintf(s, "%lu", n);
    string r(s);
    reverse(r.begin(), r.end());
    int space_inserted = 0;
    size_t how_many_spaces = r.length() / 3;

    if(r.length() % 3 != 0)
        how_many_spaces += 1;

    for(int i = 1; i < how_many_spaces; ++i)
    {
        r.insert(3 * i + space_inserted, " ");
        space_inserted += 1;
    }
    reverse(r.begin(), r.end());

    return r;
}

Do you know any better solution ?

  • 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-25T10:14:03+00:00Added an answer on May 25, 2026 at 10:14 am

    This one is different, but better is subjective. I think it’s very succinct and clear what it’s doing though:

    string number_fmt(unsigned long long n, char sep = ',') {
        stringstream fmt;
        fmt << n;
        string s = fmt.str();
        s.reserve(s.length() + s.length() / 3);
    
        // loop until the end of the string and use j to keep track of every
        // third loop starting taking into account the leading x digits (this probably
        // can be rewritten in terms of just i, but it seems more clear when you use
        // a seperate variable)
        for (int i = 0, j = 3 - s.length() % 3; i < s.length(); ++i, ++j)
            if (i != 0 && j % 3 == 0)
                s.insert(i++, 1, sep);
    
        return s;
    }
    

    Using it like

    cout << number_fmt(43615091387465) << endl;
    

    prints

    43,615,091,387,465
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just came across this by Chris Coyier - http://css-tricks.com/examples/CSSTabs/ Can anyone explain me, how
This came up recently in a class for which I am a teaching assistant.
I came across this question on an interview questions thread. Here is the question:
I came across this term - Quine (also called self-reproducing programs). Just wanted to
I came across this as I was trying to learn array and vectors in
I came across this page: Plotting Tools where I found a set of tools
I came accross this on the Mike Ash Care and feeding of singletons and
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I came across this question after searching for a ODBC or JDBC. To my
I came across this question: say given two weights 1 and 3, u can

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.