[Help] C++, how to show full decimals, i.e. 10.500 ? [answered]

orbitalfreak

Explorer
Need a little help for a program I have due tomorrow...

I need to know how to make decimal numbers display in full using C++ (devC++ from www.bloodshed.net , compiler). For instance, with:
float x1 = 1.0;

I want to code this:
cout << x1 << endl;

and have it display:
1.00000
not this:
1.0

I want it with several decimal places, not just until it terminates. Can anyone give me the function or flag to do this. I think it's something like fixed, showpos, or somethng like that.
 
Last edited:

log in or register to remove this ad


Sorry, I'm not familiar with the printf() function, I was taught only cout and ofstream. Thanks anyway. :)

Some expanded info: I know that what I'm looking for is a one-time thing. You either call a function or set a flag, and it stays for the rest of the program (well, I guess you could un-set it). It would be something introduced in an Intro to Programming course.
 

I pulled this out of Microsoft's online docs for VC++ -- which means I haven't tested this at all -- and I haven't written any C++ in years (if this isn't your homework, then use a language that's better with strings, like Perl, C#, Visual Basic.NET, or Java), but I think that

Code:
cout<<setprecision(6)<<x1<<endl;

ought to work.
 

Yea, setprecision() is what I'm looking for, but it's not working for me. I'm sure it's something trivial that I'm missing too.

And yes, this is homework, and C++ is the only thing I'm familiar with. I'm a math major, not computer science. Dropped CompSci like a hot potato; I play on computers, don't work on/with them! :)

*****

edit: Success!
cout.setf(ios::fixed);
cout << setprecision(5);
That got everything working right for me. Thanks for pointing me in the right direction! :D
 
Last edited:

orbitalfreak said:
Yea, setprecision() is what I'm looking for, but it's not working for me. I'm sure it's something trivial that I'm missing too.

And yes, this is homework, and C++ is the only thing I'm familiar with. I'm a math major, not computer science. Dropped CompSci like a hot potato; I play on computers, don't work on/with them! :)

*****

edit: Success!
cout.setf(ios::fixed);
cout << setprecision(5);
That got everything working right for me. Thanks for pointing me in the right direction! :D
The setf bit was actually in the example I looked at, I just didn't think it was necessary. Sorry about that.
 

Remove ads

Top