Formatting decimal places

A

Andrew

I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.
 
M

Marcin Kalicinski

Uzytkownik "Andrew said:
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.

Check out boost::format.

Marcin
 
J

John Harrison

Andrew said:
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.

You have to set the 'float mode' to fixed. E.g.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

cout << fixed << setprecision(1) << 1.23456789 << '\n';

cout << fixed << setprecision(2) << 1.23456789 << '\n';

cout << fixed << setprecision(3) << 1.23456789 << '\n';

cout << fixed << setprecision(4) << 1.23456789 << '\n';

cout << fixed << setprecision(5) << 1.23456789 << '\n';

}

john
 
M

Mike Wahler

Andrew said:
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.

#include <ios>
#include <iomanip>
#include <iostream>

int main()
{
double d(3.14);

std::cout << std::fixed << std::setprecision(4)
<< d << '\n';

return 0;
}

Output:

3.1400


-Mike
 
S

Siemel Naran

You have to set the 'float mode' to fixed. E.g.
cout << fixed << setprecision(1) << 1.23456789 << '\n';

cout << fixed << setprecision(2) << 1.23456789 << '\n';

The float mode is sticky, so you don't have to set it in each line, though
setting it is harmless and often a good idea for clarity if we're fixing
fixed and scientific output in the same program.

cout << fixed;
cout << setprecision(1) << 1.23456789 << '\n';
cout << setprecision(2) << 1.23456789 << '\n';
 
B

bartek

Julie said:
Boost is OT in this forum.

Why don't you like boost?

Strange, but I've seen other people not using it *just because* (i'm not
counting compiler issues that might arise with boost).
Why - I wonder.

cheers!
b
 
J

Julie

bartek said:
Why don't you like boost?

I did not express my preference for Boost in my response.

I was expressing (rather tersely) that a reference to Boost is off-topic in
this forum when there is an existing and preferred solution in C++ language.
 
B

bartek

Julie said:
I did not express my preference for Boost in my response.

I was expressing (rather tersely) that a reference to Boost is
off-topic in this forum when there is an existing and preferred
solution in C++ language.

Sorry, I overreacted to your post.

Although, I don't think that mentioning boost is off topic here at all.
Just as it's not off topic to direct an (confused?) individual to other
(more appropriate?) newsgroups, isn't it?

cheers!
b
 
D

Dave Moore

I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.

I doubt you have stated things correctly, unless your version of the
STL is broken ... an example usually helps clear up such
misunderstandings. The problem is that you also need to use the
manipulator std::fixed to specify that the field has a fixed number of
decimal places. Thus the following code

#include <iostream>
#include <iomanip>

int main() {
std::cout << std::setprecision(3);
std::cout << 1.0 << ' ' << 12.47 << ' ' << 16.89567 << std::endl;
std::cout << std::fixed;
std::cout << 1.0 << ' ' << 12.47 << ' ' << 16.89567 << std::endl;
return 0;
}

should produce:
1 12.47 16.9 // think about it .. you'll figure out why
1.0000 12.4700 16.8957

HTH, Dave Moore
 
D

Dave Moore

I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.

Sorry .. after submitting I realized that the line

std::cout << std::setprecision(3);

in my previous post should have been

std::cout << std::setprecision(4);

I guess that is why they give you that "review before posting" option
at google 8*) .. sorry for any confusion.
 
J

Jeff Schwab

Julie said:
Nope. OT is the accepted "Off Topic".

From one of my other responses:

Not a fan of irony, I see.

Why is it OT even to mention boost? Also, if you know of a "preferred
solution," why was it worth your time to criticize someone else, but not
worth your time to present the solution?
 
D

David Harmon

On Tue, 20 Apr 2004 10:41:14 -0700 in comp.lang.c++, Julie

Boost is a collection of solutions in the C++ language. Widely accepted
and frequently preferred. Not in any way off-topic that I can see.
It does happen to be the wrong answer to the question of this thread,
but that's a different matter entirely.
 
J

Julie

David said:
On Tue, 20 Apr 2004 10:41:14 -0700 in comp.lang.c++, Julie


Boost is a collection of solutions in the C++ language.
True.

Widely accepted and frequently preferred.

I don't know if that is true or not, regardless, that doesn't make it on
topic. There are plenty of other libraries out there that are equally widely
accepted and preferred, but are off topic. I'm not saying that Boost, per se,
is on or off topic, just saying that universal acceptance doesn't equate to on
topic.
Not in any way off-topic that I can see.

As I said, it is off topic because there is a perfectly suitable C++ language
solution. If there were no C++ solution, then the Boost response may have been
appropriate and on-topic.
It does happen to be the wrong answer to the question of this thread,
but that's a different matter entirely.

No, it wasn't a different matter, but entirely _the_ matter of discussion.

I know that this discussion is getting close to splitting hairs that no one
really cares about, including me, so I don't really have much more to add to
this or any other response.
 
J

Julie

Jeff said:
Not a fan of irony, I see.

I don't know what that means, so I'll leave it up to you to determine for
yourself if I'm a fan or not.
Why is it OT even to mention boost? Also, if you know of a "preferred
solution," why was it worth your time to criticize someone else, but not
worth your time to present the solution?

I wasn't specifically aware of the preferred solution, I just new that it
existed.
 
C

Claudio Puviani

David Harmon said:
On Tue, 20 Apr 2004 10:41:14 -0700 in comp.lang.c++, Julie
language.

Boost is a collection of solutions in the C++ language. Widely accepted
and frequently preferred. Not in any way off-topic that I can see.
It does happen to be the wrong answer to the question of this thread,
but that's a different matter entirely.

It's off-topic in the same way that all other third party libraries are
off-topic, regardless of how good they may or may not be. It's not part of the
language. If we open that door, the newsgroup will be flooded with threads
about boost, QT, CommonC++, blitz, MFC, ATL, M++, Rogue Wave, wxwindows, and
dozens upon dozens of others. These libraries are NOT the core standard C++
language/libraries which is the topic of this newsgroup. This isn't to say that
they're not valid and even interesting topics elsewhere, but they are out of
place here.

Claudio Puviani
 
D

Dan Bloomquist

Julie said:
I don't know what that means, so I'll leave it up to you to determine for
yourself if I'm a fan or not.

But Julie, it was a *joke*!

Best, Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top