LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ format float number output (https://www.linuxquestions.org/questions/programming-9/c-format-float-number-output-190343/)

keefaz 06-06-2004 11:42 AM

C++ format float number output
 
Hi,

I don't succeed with formating output for float number, all I want is a list like :
0.00
15.00
12.25
13.56
12.50

from float like :
0.0
15.0
12.25
13.56
12.5

nothing hard but I can not figure out how cout work, I tried :
Code:

cout.precision(3);
cout << showpoint;
cout << 0.0 << "\n" << 15.0 << "\n" << 12.25 << "\n" << 13.56 << "\n" << 12.5 << "\n";

but output is :
0.00
15.0
12.2
13.6
12.5

Any suggestion or where to find man for that will be much apprechated ;)

jedi_il 06-06-2004 11:48 AM

try
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);

keefaz 06-06-2004 11:51 AM

Maaaannny thanks ;)

The_Nerd 06-06-2004 02:16 PM

or just:

printf("%.2f", myfloat);


All times are GMT -5. The time now is 10:39 PM.