StringStream + std::string Formatting

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!
Who is Participating?
Infinity08Connect With a Mentor Commented:
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 ? :)
Infinity08Connect With a Mentor Commented:
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 :)
oxygen_728Author Commented:
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

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Infinity08Connect With a Mentor Commented:
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 ?" :)
oxygen_728Author Commented:
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!
Infinity08Connect With a Mentor Commented:
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 !!
oxygen_728Author Commented:
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
oxygen_728Author Commented:
2. Yep that looks pretty good =)

3. That is the ultimate question! Congratulations =)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.