StringStream + std::string Formatting

Posted on 2006-05-31
Last Modified: 2008-01-09
What are efficient ways to do the following?

1. ----------------------------------------------------------------
Double D = 12.3456;
Double D2 = 555.4444;

Display both of these numbers to two decimal places with the same line of code (as if in a loop)

Begin Loop
  Output some double variable to 2 decimal places (even though it might have many digits to the left)
end loop

2. -----------------------------------------------------------------
Pad a string with blanks (ex: Formatting a table (rows/columns))

3. -----------------------------------------------------------------
Well, there is no three - I thought there would be though!
Question by:oxygen_728
    LVL 53

    Assisted Solution

    1. If it has to be in a loop, then i give you one hint : arrays !
    2. is memset allowed ? Or are you looking for a more efficient solution ?
    3. I also thought there would come a third reply, but apparently that's not the case :)

    Author Comment

    1. I figured there'd be a built-in solution for this!
    2. I'm looking for an efficient solution - Run-Time is critical
    3. The answer is 42

    LVL 53

    Assisted Solution

    1. what do you mean by built-in solution ? You do know you can output multiple values in the same line of code, do you ?
    Something like :

        out << value1 << " " << value2

    2. For the most efficient solution, a lot depends on the way you're gonna use this ! One way could be to "pre-fill" the table with blanks, and then just add the values. Another could be to do the table formatting using tabs. Yet another could be to print on certain positions ... can you give a bit more info about the way this is gonna be used ?

    3. Is this jeopardy ? In that case the question is "What is the meaning of life, the universe and everything ?" :)

    Author Comment

    Regarding 1:

    I think we didn't communicate well on this point. I'd like to be able to display either variable out to 2 decimal places - If I use stringstream and set the precision to 6,  D will be displayed out to 4 decimal places and D2 will be displayed out to three.

    I'd like to use a solution that cuts numbers off at X number of decimal places instead of X number of digits.

    2. I don't know how it'll be used - I'm just preparing for a large project - I'll probably just write a quicky function to do it fast.

    3. I think we agree!
    LVL 53

    Assisted Solution

    1. I see. This might be of interest to you :

        double D = 12.3456;
        double D2 = 555.4444;

        std::cout.setf(std::ios::fixed, std::ios::floatfield);

        std::cout << D << " " << D2 << std::endl;

    2. You don't need to write a function ... memset does that already ... if you have more information about how it's gonna be used, we can maybe provide you with a more efficient way, but memset() is already quite good !!

    Author Comment

    1. I'll play around with that

    2. I'll probably just have a double column of text and want them to line-up properly -- Tabs won't work well due to strings overstepping tab boundaries

    3. I'll give it some more thought
    LVL 53

    Accepted Solution

    2. Maybe this can be of use to you then :

        std::cout << std::left << "column1" << "column2" << std::endl;

    width() sets the minimum number of printed characters (note that the maximum is not set ... if the string is longer than the specified width, the whole string will be printed). So, the above code will print two columns of 30 characters wide, left-aligned.

    3. how about another 10 million years ? :)

    Author Comment

    2. Yep that looks pretty good =)

    3. That is the ultimate question! Congratulations =)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now