38

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
  • 15,729
  • 8
  • 46
  • 54
Malik
  • 547
  • 2
  • 5
  • 12

10 Answers10

43
#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
  • 24,264
  • 10
  • 66
  • 98
  • 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
7
#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
  • 7,689
  • 10
  • 38
  • 58
Loyce
  • 87
  • 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
  • 833
  • 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
3

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
  • 989
  • 1
  • 14
  • 24
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_
  • 385
  • 4
  • 8
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

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
  • 71
  • 1
  • 5