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

Came across this error today. Wondering if anyone can tell me what it means:
This came up from this answer to a previous question of mine . Is
This came up in Hidden features of Python , but I can't see good
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
This came up in a conversation I was having online, and it occured to
This came up recently in a class for which I am a teaching assistant.
I came across this article written by Andrei Alexandrescu and Petru Marginean many years
I came across this recently, up until now I have been happily overriding the
I came across this class while reading a C# book and have some questions.
I came in this morning, fired up my app in the same way as

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.