2

I know how to use printf() to format output of float, for example:

float i = 1;
printf("%.2f", i);

but how to format the output using cout to output 2 digits after "."?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

7 Answers7

7

Following will do:

std::cout<<std::fixed<<std::setprecision(2)<<i;

You will also need to include iomanip

Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
2
#include <iostream>
#include <iomanip>

using namespace std;
int main() {
    cout << setprecision(2) << setiosflags(ios::fixed) << 3.141592 << endl;
}

use setprecision().

mattn
  • 7,571
  • 30
  • 54
1

A red herring is to use std::setprecision on its own:

float f = 3.1415;
std::cout << std::setprecision(2) << f;

However, "precision" is not the same as "number of decimal places", as shown in this example.

Fortunately, and somewhat confusingly, switching the stream to "fixed-point" display changes the meaning of std::setprecision to something more akin to "setDecimalPlaces".

So, you can write this:

float f = 3.1415;
std::cout << std::fixed << std::setprecision(2) << f;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

Boost format helps:

std::cout << boost::format("%.2f") % i;
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

hope this helps.........

#include <iostream>
#include <iomanip>
using namespace std;

int main () {
  double f =3.14159;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  cout << fixed;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  return 0;
}

output..

> 3.1416
> 3.14159
> 3.14159
> 3.141590000
  • cplusplus.com is a ghastly "resource". – Lightness Races in Orbit Jun 20 '11 at 09:10
  • @Abhimanyu: (a) You didn't format your post correctly. I edited it for you. (b) No, it's not sufficient. My example of your code clearly demonstrates that. – Lightness Races in Orbit Jun 20 '11 at 09:13
  • it is doing the same thing it is supposed to do......displaying the number of digits you want to see....let me edit my post... – Abhimanyu Srivastava Jun 20 '11 at 09:14
  • @Abhimanyu: No, it's not. In the codepad example I ask for 2dp and get 1. This is clearly displayed on the screen. You are confusing "precision" with "decimal places". `std::fixed` changes the meaning of `std::setprecision` (somewhat confusingly) to "setdecimalplaces", but your answer does not employ this. – Lightness Races in Orbit Jun 20 '11 at 09:15
  • @tomalak....that is why i added the link for reference.....what i mentioned was for calculating the roundoff to the digits u want to see.....and if you want to do something else thats why the link was for... – Abhimanyu Srivastava Jun 20 '11 at 09:18
  • @Abhimanyu: So you wrote an answer that _does not answer the question_, including code that does not do what the OP wants it to do, reasoning that the link you provided to a sub-par internet resource happens to contain the real answer somewhere therein? You also wrote "number of digits after decimal u want" in a previous edit, showing that in fact you were _not_ aware of this at all. – Lightness Races in Orbit Jun 20 '11 at 09:20
  • @tomalak.....now u r happy.........whats the point posting it from another site...?the ques was simple and needed a simple answer and didnt find the need write the whole thing coz if the person is program a simple hint could be sufficient enough to help him....we don't really need to spoonfeed the.... – Abhimanyu Srivastava Jun 20 '11 at 09:23
  • @Abhimanyu: Regardless, you posted code that basically said "insert number of decimal places required here", that then did not display the number of decimal places entered. – Lightness Races in Orbit Jun 20 '11 at 09:36
  • and i have finally edited also.........there was a slight msitake which i edited the first tym itself....but still u were able to deduce that i know nothing about it....how come??? – Abhimanyu Srivastava Jun 20 '11 at 09:38
  • @Abhimanyu: You also wrote "number of digits after decimal u want" in a previous edit, showing that in fact you were not aware of this at all. I'm not going to comment on this thread again. – Lightness Races in Orbit Jun 20 '11 at 09:48
  • 1
    Chill down guyz. We are here to make a better world not to fight over it. :) – Jayesh Jun 20 '11 at 09:48
0
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double x = 800000.0/81.0;
    cout << setiosflags(ios::fixed) << setprecision(2) << x;
    return 0;
}
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
0
cout << ffmt(0, 2) << i;

Where ffmt is defined as:

class ffmt
{
    int width;
    int precision;
public:
    ffmt( int width, int precision )
        : width( width )
        , precision( precision )
    {
    }
    friend std::ostream& operator<<(
        std::ostream& dest,
        ffmt const& manip )
    {
        dest.setf( std::ios_base::fixed, std::ios_base::floatfield );
        dest.width( width );
        dest.precision( precision );
    }
};

You can also extend this to restore format state at the end of the full expression; it's not too important for floating point, because most of the time, you'll use the manipulator in front of each output, but a manipulator which leaves the stream outputting hex is likely to lead to surprising output later.

As a general rule, except for quicky demo or test programs, you almost never use the standard manipulators, other that std::setw. For that matter, in a lot of applications, you'll even avoid things like ffmt, in favor of specific manipulators for each semantic value you have.

James Kanze
  • 150,581
  • 18
  • 184
  • 329