Malik
Malik

Reputation: 615

How to use setprecision in C++

I am new in C++ , i just want to output my point number up to 2 digits. just like if number is 3.444, then the output should be 3.44 or if number is 99999.4234 then output should be 99999.42, How can i do that. the value is dynamic. Here is my code.

#include <iomanip.h>
#include <iomanip>
int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
}

but its giving me an error, undefined fixed symbol.

Upvotes: 44

Views: 386453

Answers (13)

sashjack
sashjack

Reputation: 11

Simpler way...

#include <iostream>
#include <iomanip>

int main() 
{
    double num1 = 3.12345678;
    std::cout << std::fixed << std::setprecision(2) << num1 << "\n";
}

Upvotes: 1

SamuelC00
SamuelC00

Reputation: 61

The syntax for setting the fixed and precision is the following (using dot notation with cout rather than <<):

#include <iostream>
using namespace std;    

int main(int argc, const char * argv[]) {
    double num1 = 3.12345678;
    cout.setf(ios::fixed);
    cout.precision(2);
    // 3.12
    cout << num1 << endl;
    return 0;
}

Upvotes: 0

AnuragD
AnuragD

Reputation: 1

Here is the solution that works with any precision setting. To get precision to 2 decimal places in sstream here is my code snippet. Examples 3.444 will be 3.44, 1.10 will be 1.10, and 1.3245 will be 1.32

#include<sstream>

std::wostringtream stream;
float value = 3.14159;
// To get the output to 2 decimal places
stream.precision(2);
stream << std::fixed << value;
std::wcout<<stream.str()<<L"\n";

// The output will be 3.14

Upvotes: 0

Gesy Darati
Gesy Darati

Reputation: 113

This is what worked for me, I found this in the CPP book by Anthony.

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
  • First set the flags to fixed.
  • Then set to show decimal points.
  • Lastly set the number of decimal places you want after the comma.

Upvotes: 1

dhananjay dhawale
dhananjay dhawale

Reputation: 11

If you don't want to print the value of float/double but store it in a string and then print then you can use this. Useful when you want to use set precision without using cout in c++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    float z = 9.9999;
    string s = "";
    stringstream ss;
    ss << fixed << setprecision(2) << z;
    s += ss.str();
    cout << s << "\n";
    return 0;
}

Output : 10.00

Upvotes: 1

ANSHUMAN KARMAKAR
ANSHUMAN KARMAKAR

Reputation: 51

std::cout.precision(2);
std::cout<<std::fixed;

when you are using operator overloading try this.

Upvotes: 4

SYEDABUTHAHIR
SYEDABUTHAHIR

Reputation: 1

#include <iostream>
#include <iomanip>
 
int main(void) 
{
    float value;
    cin >> value;
    cout << setprecision(4) << value;
    return 0;
}

Upvotes: 0

confused_
confused_

Reputation: 1681

#include <bits/stdc++.h>                        // to include all libraries 
using namespace std; 
int main() 
{ 
double a,b;
cin>>a>>b;
double x=a/b;                                 //say we want to divide a/b                                 
cout<<fixed<<setprecision(10)<<x;             //for precision upto 10 digit 
return 0; 
} 

input: 1987 31

output: 662.3333333333 10 digits after decimal point

Upvotes: 1

pkthapa
pkthapa

Reputation: 1071

Below code runs correctly.

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

int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
}

Upvotes: 2

Loyce
Loyce

Reputation: 85

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

You can enter the line using namespace std; for your convenience. Otherwise, you'll have to explicitly add std:: every time you wish to use cout, fixed, showpoint, setprecision(2) and endl

int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
return 0;
}

Upvotes: 6

user7122338
user7122338

Reputation:

Replace These Headers

#include <iomanip.h>
#include <iomanip>

With These.

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

Thats it...!!!

Upvotes: 2

piokuc
piokuc

Reputation: 26184

#include <iomanip>
#include <iostream>

int main()
{
    double num1 = 3.12345678;
    std::cout << std::fixed << std::showpoint;
    std::cout << std::setprecision(2);
    std::cout << num1 << std::endl;
    return 0;
}

Upvotes: 49

aliasm2k
aliasm2k

Reputation: 901

The answer above is absolutely correct. Here is a Turbo C++ version of it.

#include <iomanip.h>
#include <iostream.h>

void main()
{
    double num1 = 3.12345678;
    cout << setiosflags(fixed) << setiosflags(showpoint);
    cout << setprecision(2);
    cout << num1 << endl;
}

For fixed and showpoint, I think the setiosflags function should be used.

Upvotes: 3

Related Questions