StringStream + std::string Formatting

Posted on 2006-05-31
Medium Priority
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
  • 4
  • 4
LVL 53

Assisted Solution

Infinity08 earned 2000 total points
ID: 16805275
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

ID: 16805314
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

Infinity08 earned 2000 total points
ID: 16805395
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 ?" :)

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 16805475
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

Infinity08 earned 2000 total points
ID: 16805593
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

ID: 16805809
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

Infinity08 earned 2000 total points
ID: 16805952
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

ID: 16805991
2. Yep that looks pretty good =)

3. That is the ultimate question! Congratulations =)


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

750 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