Print the correct number of decimal points with cout

I have a list of float values ​​and I want to print them using cout with two decimal places.

For example:

 10.900 should be printed as 10.90 1.000 should be printed as 1.00 122.345 should be printed as 122.34 

How can i do this?

( setprecision doesn't seem to help with this.)

+94
c ++
May 6 '11 at 5:12
source share
12 answers

With <iomanip> you can use std::fixed and std::setprecision

Here is an example

 #include <iostream> #include <iomanip> int main() { double d = 122.345; std::cout << std::fixed; std::cout << std::setprecision(2); std::cout << d; } 

And you will get a way out

 122.34 
+142
May 6 '11 at 5:18
source share

You were almost there, you should also use std :: fixed, see http://www.cplusplus.com/reference/iostream/manipulators/fixed/

 #include <iostream> #include <iomanip> int main(int argc, char** argv) { float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 }; std::cout << std::setprecision(2) << std::fixed; for(int i = 0; i < 6; ++i) { std::cout << testme[i] << std::endl; } return 0; } 

outputs:

 0.12 1.23 12.35 123.45 1234.50 12345.00 
+35
May 6 '11 at 5:31
source share

setprecision(n) applies to the whole number, not the fractional part. You need to use the fixed point format to apply it to the fractional part: setiosflags(ios::fixed)

+15
May 6 '11 at 5:20 a.m.
source share

Simplify the accepted answer

A simplified example:

 #include <iostream> #include <iomanip> int main() { double d = 122.345; std::cout << std::fixed << std::setprecision(2) << d; } 

And you will get the result

 122.34 

Reference:

+7
Jun 02 '15 at 6:33
source share

I am having a problem with integers if you want to coordinate formatting.

Rewrite for completeness:

 #include <iostream> #include <iomanip> int main() { // floating point formatting example double d = 122.345; cout << std::fixed << std::setprecision(2) << d << endl; // Output: 122.34 // integer formatting example int i = 122; cout << std::fixed << std::setprecision(2) << double(i) << endl; // Output: 122.00 } 
+4
Sep 02 '17 at 3:56 on
source share

You must set the fixed mode to "float".

 float num = 15.839; // this will output 15.84 std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl; 
+2
May 6 '11 at 5:37
source share

I had a similar problem in a coding contest, and that's how I dealt with it. Setting accuracy 2 for all double values

First add header to use setprecision

#include <iomanip>

Then adding the following code to our main

  double answer=5.9999; double answer2=5.0000; cout<<setprecision(2)<<fixed; cout <<answer << endl; cout <<answer2 << endl; 

Output:

 5.99 5.00 

You need to use the fix for recording 5.00, so your output will not be at 5.00.

Link to a short link for links that I am adding, which is useful

+2
Jul 25 '17 at 17:49
source share

To set fixed 2 digits after the decimal point, use them first:

 cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); 

Then print the double values.

That's an example:

 #include <iostream> using std::cout; using std::ios; using std::endl; int main(int argc, char *argv[]) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); double d = 10.90; cout << d << endl; return 0; } 
+1
Nov 28 '16 at 10:39
source share
 #include<stdio.h> int main() { double d=15.6464545347; printf("%0.2lf",d); } 
0
Mar 26 '17 at 18:08
source share

The user enters a floating number (for example, 345.547778) The output value should be 345.54 (not 345.55 without rounding, only accurate to 2 decimal places). Used setprecision with fixed and used accuracy. Doesn't seem to work ... help ??

0
Apr 03 '19 at 18:09
source share

Small point; put in title

following:

using the std namespace;

then

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

simplified to

cout <fixed <setprecision (2) <q;

-one
Oct 24 '16 at 22:43
source share

This example uses a matrix.

 cout<<setprecision(4)<<fixed<<m[i][j] 
-3
Mar 09 '16 at 13:41
source share



All Articles