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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:33:53+00:00 2026-05-14T23:33:53+00:00

In C++ I need string representations of integers with leading zeroes, where the representation

  • 0

In C++ I need string representations of integers with leading zeroes, where the representation has 8 digits and no more than 8 digits, truncating digits on the right side if necessary. I thought I could do this using just ostringstream and iomanip.setw(), like this:

int num_1 = 3000;
ostringstream out_target;

out_target << setw(8) << setfill('0') << num_1;
cout << "field: " << out_target.str() << " vs input: " << num_1 << endl;

The output here is:

field: 00003000 vs input: 3000

Very nice! However if I try a bigger number, setw lets the output grow beyond 8 characters:

int num_2 = 2000000000;
ostringstream out_target;

out_target << setw(8) << setfill('0') << num_2;
cout << "field: " << out_target.str() << " vs input: " << num_2 << endl;
out_target.str("");

output:

field: 2000000000 vs input: 2000000000

The desired output is “20000000”. There’s nothing stopping me from using a second operation to take only the first 8 characters, but is field truncation truly missing from iomanip? Would the Boost formatting do what I need in one step?

  • 1 1 Answer
  • 3 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-14T23:33:54+00:00Added an answer on May 14, 2026 at 11:33 pm

    I can’t think of any way to truncate a numeric field like that. Perhaps it has not been implemented because it would change the value.

    ostream::write() allows you to truncate a string buffer simply enough, as in this example…

        int num_2 = 2000000000;
        ostringstream out_target;
    
        out_target << setw(8) << setfill('0') << num_2;
        cout << "field: ";
        cout.write(out_target.str().c_str(), 8);
        cout << " vs input: " << num_2 << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do an HTTP post of information including String byte representations of
I need to parse an RFC 2822 string representation of a date in Java.
I'm using Javascript along with Date.js and need to translate string representations of times.
I need to generate a GUID and save it via a string representation. The
I have a String representation of a date that I need to create a
I'm getting TypeError: coercing to Unicode: need string or buffer, int found This is
I need split string by commas and spaces, but ignore the inside quotes, single
I need a string->string mapping to be used at runtime (think NSDictionary ), except
I need a string splitted into groups of characters in ActionScript like this: var
For a JUnit test I need a String which consists of multiple lines. But

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.