209

I have a list of float values and I want to print them with cout with 2 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 in this.)

Arun A S
  • 6,421
  • 4
  • 29
  • 43
thameera
  • 9,168
  • 9
  • 37
  • 38

13 Answers13

313

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 output

122.34
ismail
  • 46,010
  • 9
  • 86
  • 95
beduin
  • 7,943
  • 3
  • 27
  • 24
  • 20
    why do you used "std:fixed" in program? – Vilas Joshi Jun 05 '18 at 16:59
  • 7
    A useful header can be defined for this: `#define FIXED_FLOAT(x) std::fixed < – Udayraj Deshmukh Aug 25 '18 at 02:03
  • 25
    @VilasJoshi, setprecision set the number of digits after the decimal, if there are 5 digits and we use setprecision(2) we will get 2 digits , but if there are 0 digits it will show none, using fixed we fix that much digits have to be shown so 5 will be represented as 5.00 no 5 – vaibnak Oct 18 '18 at 13:33
  • 7
    @vaibnak That's misleading to the point of wrong. `setprecision` sets "the number of digits", which depending on the state that `std::fixed` sets is either "significant digits", "digits after the decimal place" or "digits after the *hexadecimal* place" – Caleth Apr 06 '21 at 16:00
  • Thanks for the heads up Caleth. Note for other people that "n place after decimal point" is indeed using setprecision in fixed – RexYuan Apr 07 '21 at 18:17
  • Doesn't work for negative numbers, like -0.4 with a precision of 2 – KarmaPenny May 27 '21 at 00:54
59

You were nearly there, need to use std::fixed as well, refer 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
Vusak
  • 1,420
  • 9
  • 12
21

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

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
19

Simplify the accepted answer

Simplified example:

#include <iostream>
#include <iomanip>

int main()
{
    double d = 122.345;
    std::cout << std::fixed << std::setprecision(2) << d;
}

And you will get output

122.34

Reference:

einverne
  • 6,454
  • 6
  • 45
  • 91
8
#include<stdio.h>
int main()

{

 double d=15.6464545347;

printf("%0.2lf",d);

}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
saurav52
  • 99
  • 1
  • 2
7

I had this similar problem in a coding competition and this is how I handled it. Setting a precision of 2 to all double values

First adding the header to use setprecision

#include <iomanip>

Then adding the following code in 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 fixed for writing 5.00 thats why,your output won't come for 5.00.

A short reference video link I'm adding which is helpful

Hargun Singh
  • 544
  • 7
  • 19
4

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

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

Then print your double values.

This is 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;
}
Hamid Rouhani
  • 2,309
  • 2
  • 31
  • 45
3
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;

int main() {
    int a;
    long int b;
    char c;
    float d;
    double e;
    cin>>a>>b>>c>>d>>e;
    cout<<a<<"\n"<<b<<"\n"<<c<<"\n";
    cout<<fixed<<setprecision(3)<<d<<"\n";
    cout<<fixed<<setprecision(9)<<e;
    return 0;
}

Simple code would help you surely!

2

You have to set the 'float mode' to fixed.

float num = 15.839;

// this will output 15.84
std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;
Eric Z
  • 14,327
  • 7
  • 45
  • 69
2

with templates

#include <iostream>

// d = decimal places
template<int d> 
std::ostream& fixed(std::ostream& os){
    os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
    os.precision(d); 
    return os; 
}

int main(){
    double d = 122.345;
    std::cout << fixed<2> << d;
}

similar for scientific as well, with a width option also (useful for columns)

// d = decimal places
template<int d> 
std::ostream& f(std::ostream &os){
    os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
    os.precision(d); 
    return os; 
}

// w = width, d = decimal places
template<int w, int d> 
std::ostream& f(std::ostream &os){
    os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
    os.precision(d); 
    os.width(w);
    return os; 
}

// d = decimal places
template<int d> 
std::ostream& e(std::ostream &os){
    os.setf(std::ios_base::scientific, std::ios_base::floatfield); 
    os.precision(d); 
    return os; 
}

// w = width, d = decimal places
template<int w, int d> 
std::ostream& e(std::ostream &os){
    os.setf(std::ios_base::scientific, std::ios_base::floatfield); 
    os.precision(d); 
    os.width(w);
    return os; 
}

int main(){
    double d = 122.345;
    std::cout << f<10,2> << d << '\n'
        << e<10,2> << d << '\n';
}
QuentinUK
  • 2,997
  • 21
  • 20
-1

Easiest way:

#include<stdio.h> 
int main() {
double d = 122.345;
printf(".2%lf",d); 
}
  • This answer is basically the same answer as the one by saurav52, does not use `cout` as per OPs question, and includes a typo in the format string. – JDS Sep 18 '22 at 00:46
-3

Just a minor point; put the following in the header

using namespace std;

then

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

becomes simplified to

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

-6

this an example using a matrix.

cout<<setprecision(4)<<fixed<<m[i][j]
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185