Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   float - cut down digts? (http://www.programmingforums.org/showthread.php?t=12524)

programmingnoob Feb 7th, 2007 1:15 AM

float - cut down digts?
 
you know if you do float x = 12/12.0;
it'll give you x = 1.00000

now what do i do if i want to cout the result at 1.0

do I have to convert x into a string and take out "characters" or what? that just doesnt sound very sophiscated
i feel there is better approach to it?

Game_Ender Feb 7th, 2007 2:39 AM

Try this:
:

std::cout << std::setprecision(1) << x <<std::endl;

UnKnown X Feb 9th, 2007 6:09 PM

That's fairly tedious, though. Isn't there a quicker way, aside from the C-style printf()?

Eoin Feb 9th, 2007 6:58 PM

Boost.Format offers printf() style with type-safety.

rsnd Feb 11th, 2007 4:06 PM

Quote:

Originally Posted by UnKnown X (Post 123732)
That's fairly tedious, though. Isn't there a quicker way, aside from the C-style printf()?

yeah...at the top of the program justa fter those std headers go:
:

using namespace std;

and then go:
:

cout << setprecision(1) << x << endl;



an even easier approach is coding your own fraction printing function.

DaWei Feb 11th, 2007 4:13 PM

"using namespace std;" is really for small exemplar programs and isn't really a good recommendation for robust programming of a serious nature. It introduces a lot of symbols; in a large program, you're likely to duplicate some (how many 'max' labels have you seen introduced by the coder). One alternative to always qualifying the label is to introduce just a few commonly used members, such as
:

using std::cout;
using std::cin;
using std::string;
etc.


bl00dninja Feb 11th, 2007 8:01 PM

do what dawei said. i'm drunk right now.


All times are GMT -5. The time now is 1:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC