41

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.

suspectus
  • 16,548
  • 8
  • 49
  • 57
Malik
  • 585
  • 2
  • 6
  • 13

12 Answers12

47
#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;
}
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • class name in function main() Error NONAME00.CPP 7: Statement missing ; in function main() Error NONAME00.CPP 8: Type qualifier 'std' must be a struct or class name in function main() Error NONAME00.CPP 8: Statement missing ; in function main() Error NONAME00.CPP 9: Type qualifier 'std' must be a struct or class name in function main() Error NONAME00.CPP 9: Statement missing ; in function main() Warning NONAME00.CPP 10: Function should return a value in function main() Warning NONAME00.CPP 10: 'num1' is assigned a value that is never used in function main().... I am facing this error – Malik Mar 19 '14 at 19:12
  • 3
    Are you sure you copied the include statements from the answer verbatim? – piokuc Mar 19 '14 at 19:45
  • I think the complaint being referred to is the lack of a return on main. – Pradyot Apr 28 '15 at 10:42
  • 3
    @Pradyot FYI, `int main()` can be left without a return value at which point it defaults to returning 0. – piokuc May 26 '15 at 09:16
  • 2
    If i dont want to do a cout. just want to set the precision of `num1` to 3 decimal places . How do I do that ? – TheWaterProgrammer Jul 08 '18 at 22:12
  • 3
    @Game_Of_Threads You can't. There is no such thing. The precision of a `double` is fixed; it is an inherent part of the datatype. What we're doing here is altering the _apparent_ precision when we print the number for human consumption. – Lightness Races in Orbit Nov 19 '18 at 14:05
6
#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;
}
LuFFy
  • 8,799
  • 10
  • 41
  • 59
Loyce
  • 85
  • 1
  • 2
4
std::cout.precision(2);
std::cout<<std::fixed;

when you are using operator overloading try this.

3

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.

aliasm2k
  • 883
  • 6
  • 12
  • 2
    You can use [`std::fixed`](http://en.cppreference.com/w/cpp/io/manip/fixed) and [`std::showpoint`](http://en.cppreference.com/w/cpp/io/manip/showpoint) instead, just like the other answers show. But, if you are going to use `std::setioflags()`, at least combine the flags together, eg: `cout << setiosflags(fixed | showpoint) << ...` – Remy Lebeau Nov 29 '17 at 00:00
2

Replace These Headers

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

With These.

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

Thats it...!!!

1

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;
}
pkthapa
  • 1,029
  • 1
  • 17
  • 27
1
#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

confused_
  • 1,133
  • 8
  • 10
1

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.
Gesy Darati
  • 113
  • 1
  • 6
0
#include <iostream>
#include <iomanip>
 
int main(void) 
{
    float value;
    cin >> value;
    cout << setprecision(4) << value;
    return 0;
}
  • 2
    Don't forget about the use of the namespace `std`. If possible, please describe your answer, posting only code is not a good answer. – cyberdecker Jul 27 '20 at 13:08
0

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

0

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
AnuragD
  • 1
  • 1
0

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;
}
SamuelC00
  • 61
  • 6
  • 1
    This answer might provide a correct solution, but it would be helpful to add some textual information on the usage of the code using [edit]. – Ruli Jan 10 '23 at 14:16