I want to globally set the ouptut precision to 2 decimal places.
I already tried to use iomanip and setprecision, however I keep getting output with 'e' in it.
This is my example code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double pay=16.78;
double hours;
double total;
cout.precision(2);
cout << "Please enter how many hours you worked : " << endl;
cin >> hours;
total=hours*pay;
cout << "You earned: " << total << endl;
}
You can use something like this:
double pay = 393.2993;
std::cout << std::fixed << std::setprecision(2) << pay;
You will need to include iomanip for this to work.
#include <iomanip>
I'm not familiar with "cout.precision(2)". I would use std::setprecision(4) if I wanted my output to have 4 significant figures.
If your e is a positive value, you cannot ride of them because your value is too large. This code
std::cout << std::setprecision(3) << 3e45;
//output
3e+45
If it's a negative number, your precision is not enough. Like the following code
std::cout << std::setprecision(3) << 3e-45; //
//output
3e-45
A simple way will be to use std::fixed
std::cout << std::fixed << std::setprecision(3) << 3.63e-2;
//output
0.036
But the disavantage you have with the std::fixed is it shows the zero after the last none-zero number until it reach the setprecision qunatity you set previously.
Related
This example program was created with the sole purpose of showing what setprecision and setw does. I dont understand the purpose of the third line that says "setprecision(5)". I commented the line out to see the difference but it looks the exact same. Is there no purpose?
cout << "\nSales Figures\n";
cout << "-------------\n";
cout << setprecision(5);
cout << "Day 1: " << setw(8) << day1 << endl;
cout << "Day 2: " << setw(8) << day2 << endl;
cout << "Day 3: " << setw(8) << day3 << endl;
cout << "Total: " << setw(8) << total << endl;
The setprecision() function is part of the "iomanip" library and is used when you need to output a float to a certain number of decimal places. This is good for displaying money amounts and other things that typically are shown with a set number of digits after the decimal point (even if those digits are 0).
Say you have a float called price: If you stored 10.0 in that float, C++ would not know how many decimal points to output when you print into the screen; setprecision(2) would make the output 10.00.
You can find the documentation at this link: https://cplusplus.com/reference/iomanip/setprecision/.
It includes the following code as an example of how setprecision() works.
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.1415
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.14159
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.14159
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.141590000
return 0;
}
Note that setprecision() is only applicable to data types with decimal points such as floats and doubles.
I am trying to convert a string decimal number into a double, however when I use the atof() function, my number ends up rounding to the whole number.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
The output is:
135428
I want:
135427.7
cout does that, not atof().
More precisely, operator<<, which inserts the formated data into the std::ostream.
You can use std::setprecision() from the <iomanip> standard library to print the decimals:
cout << setprecision(7) << r << endl;
or
cout << fixed << setprecision(1) << r << endl;
If you want to print the whole 135427.7000:
cout << fixed << setprecision(4) << r << endl;
This question already has answers here:
Printing the correct number of decimal points with cout
(13 answers)
Closed 6 years ago.
Similar topic is already discussed in the forum. But I have some different problem in following code:
double total;
cin >> total;
cout << fixed << setprecision(2) << total;
If I give input as 100.00 then program prints just 100 but not 100.00
How can I print 100.00?
cout << fixed << setprecision(2) << total;
setprecision specifies the minimum precision. So
cout << setprecision (2) << 1.2;
will print 1.2
fixed says that there will be a fixed number of decimal digits after the decimal point
cout << setprecision (2) << fixed << 1.2;
will print 1.20
It is possible to print a 15 decimal number in C++ using the following:
#include <iomanip>
#include <iostream>
cout << fixed << setprecision(15) << " The Real_Pi is: " << real_pi << endl;
cout << fixed << setprecision(15) << " My Result_Pi is: " << my_pi << endl;
cout << fixed << setprecision(15) << " Processing error is: " << Error_of_Computing << endl;
cout << fixed << setprecision(15) << " Processing time is: " << End_Time-Start_Time << endl;
_getch();
return 0;
The easiest way to do this, is using cstdio's printf. Actually, i'm surprised that anyone mentioned printf! anyway, you need to include the library, like this...
#include<cstdio>
int main() {
double total;
cin>>total;
printf("%.2f\n", total);
}
This will print the value of "total" (that's what %, and then ,total does) with 2 floating points (that's what .2f does). And the \n at the end, is just the end of line, and this works with UVa's judge online compiler options, that is:
g++ -lm -lcrypt -O2 -pipe -DONLINE_JUDGE filename.cpp
the code you are trying to run will not run with this compiler options...
Using header file stdio.h you can easily do it as usual like c. before using %.2lf(set a specific number after % specifier.) using printf().
It simply printf specific digits after decimal point.
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
double total=100;
printf("%.2lf",total);//this prints 100.00 like as C
}
This will be possible with setiosflags(ios::showpoint).
Is there any way in C++ (or boost lib) to show a given number digits of fraction part? But I don't want to print the trailing 0 in fraction part (eg. 1.000, 1.500). See this case:
cout << std::setprecision(3) << 5.0/7.0 << endl; // 0.714
cout << std::setprecision(3) << 12.0/7.0 << endl; // 1.71
cout << std::setprecision(3) << 7.0/7.0 << endl; // 1
cout << std::setprecision(3) << 10.5/7.0 << endl; // 1.5
The problem is setprecision prints line 1 and line 2 differently, where I want to have both lines print 0.714 and 1.714. And still keep line 3 and line 4 1 and 1.5.
How about something like:
#include <cmath>
using namespace std;
cout << setprecision(ceil(log10(floor(x))+3) << x;
Not exactly fast, but the idea is to figure out how many digits the integer part of x requires, then add the number of decimal places you're interested in to that. You could even write your own manipulator to do that if you were really serious about it.
I am taking a C++ course right now and have completed my final assignment. However there is one thing that is bugging me:
Though I have the correct outputs for the testing on a particular output, the basepay should be 133.20 and it is displaying as 133.2. Is there a way to have this display the extra 0 rather than leaving it off?
Anyone know if it's possible and how to do it? Thank you in advance
My code is below:
cout<< "Base Pay .................. = " << basepay << endl;
cout<< "Hours in Overtime ......... = " << overtime_hours << endl;
cout<< "Overtime Pay Amount........ = " << overtime_extra << endl;
cout<< "Total Pay ................. = " << iIndividualSalary << endl;
cout<< endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" <<endl;
cout<< "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" <<endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" <<endl;
cout<< "%%%% Total Employee Salaries ..... = " << iTotal_salaries <<endl;
cout<< "%%%% Total Employee Hours ........ = " << iTotal_hours <<endl;
cout<< "%%%% Total Overtime Hours......... = " << iTotal_OvertimeHours <<endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
If you want to do it in a C++ way, and you can compile with C++11 flags, you can use the standard library:
// Note: the value in cents!
const int basepay = 10000;
// Create a stream and imbue it with the local configuration.
std::stringstream ss;
ss.imbue(std::locale(""));
// The stream contains $100.00 (assuming a en_US locale config)
ss << std::showbase << std::put_money(basepay);
Example here.
What advantages have this approach?
It uses the local configuration, so the output will be coherent in any machine, even for the decimal separator, thousand separator, money symbol and decimal precission (if needed).
All the format effort is already done by the std library, less work to do!
use cout.precision to set precision, and fixed to toggle fixed-point mode:
cout.precision(2);
cout<< "Base Pay .................. = " << fixed << basepay << endl;
Yes, this is possible to do using stream manipulators. For example, set output to fixed floating-point notation, define precision (2 in your case) and define the fill character to '0':
#include <iostream>
#include <iomanip>
int main()
{
double px = 133.20;
std::cout << "Price: "
<< std::fixed << std::setprecision(2) << std::setfill('0')
<< px << std::endl;
}
In case you prefer a C-style formatting, here is an example of using printf() to achieve the same:
#include <cstdio>
int main()
{
double px = 133.20;
std::printf("Price: %.02f\n", px);
}
Hope it helps. Good Luck!
Check this out:
int main()
{
double a = 133.2;
cout << fixed << setprecision(2) << a << endl;
}
Output
133.20
You can change the cout properties:
cout.setf(ios::fixed);
cout.precision(2);`
now cout << 133.2; will print 133.20
You need to take a look at precision and fixed.
#include <iostream>
int main()
{
double f = 133.20;
// default
std::cout << f << std::endl;
// precision and fixed-point specified
std::cout.precision(2);
std::cout << std::fixed << f << std::endl;
return 0;
}