While preparing for my first ever coding interview I found a question where I was requested to print a double number with an absolute or relative error of 10^(-6).
How can I do something like that with cout?
double test_var; // Holding Some value which changes according to my program.
cout << test_var << endl;
Plus, In the expected output I saw numbers like 1000.00 how can I print those .00 too?
You could have used std::setprecision.
#include <iostream>
#include <iomanip>
int main()
{
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n'; //prints 3.1416
std::cout << std::setprecision(9) << f << '\n'; //prints 3.14159
std::cout << std::fixed; //Modifies the default formatting for floating-point input/output.
std::cout << std::setprecision(5) << f << '\n'; //prints 3.1416
std::cout << std::setprecision(9) << f << '\n'; //prints 3.141590000
}
so i think its complicated with those set or fixes
there is a simple way to acheive your goal
printf("%.6lf",test_var);
so %.6lf means to keep 6 demicals of the varible
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.
This question already has answers here:
Correct use of std::cout.precision() - not printing trailing zeros
(3 answers)
Closed 6 years ago.
I am trying to print double and integers as double. For doing so, I wrote the following program:
int main()
{
string object="1";
std::stringstream objectString;
objectString << std::setprecision(8) << atof(object.c_str());
cout<<"ObjectString="<<objectString.str()<< " "<<std::setprecision(10) << double(atof(object.c_str())) <<"\n";
}
I expected the output to be:
ObjectString=1.0 1.0
However, I am getting the output as:
ObjectString=1 1
Can someone please suggest as to where am I going wrong?
To force trailing zeros, use std::fixed:
std::string object = "1";
std::stringstream objectString;
objectString << std::fixed << std::setprecision(8) << atof(object.c_str());
std::cout << "ObjectString=" << objectString.str() << " ";
std::cout << std::fixed << std::setprecision(10) << double(atof(object.c_str())) << "\n";
Output:
ObjectString=1.00000000 1.0000000000
The way to force the output to always show the decimal point is to use std::showpoint:
#include <iostream>
#include <iomanip>
int main() {
double d = 1.0;
std::cout << std::setprecision(1) << d << '\n';
std::cout << std::setprecision(1) << std::showpoint << d << '\n';
return 0;
}
[temp]$ ./a.out
1
1.
[temp]$
This question already has answers here:
Set back default floating point print precision in C++
(5 answers)
Closed 7 years ago.
I have search the web but couldn't find what I need.
Some people recommend using
streamsize ss = std::cout.precision();
but I couldn't get it to work.
How do I set a double value back to the original state after setprecision?
#include <iostream>
using namespace std;
int main()
{
double a;
cout << "enter a double value: ";
cin >> a;
cout << "your value in 3 decimals is " << setprecision(3) << fixed << a << endl;
cout << "your original value is " << a << endl;
return 0;
}
Obviously the code above will not return the original value of a.
My intended output is: if user enter 1.267432
your value in 3 decimals is 1.267
your original value is 1.267432
How do I set a double value back to the original state after
setprecision?
To do so, you have to get the precision before you use setprecision(). In your question you already mentioned it by the following line:
streamsize ss = std::cout.precision();
but I couldn't get it to work.
Here how you use it:
streamsize ss = std::cout.precision();
double a = 1.267432;
std::cout << "a = " << a << '\n';
std::cout.precision (3);
std::cout << "a becomes = " << a << '\n';
std::cout.precision (ss);
std::cout << "Original a= " << a << '\n';
And the output will be like:
a = 1.26743
a becomes = 1.27
Original a= 1.26743
Reference: setprecision.
Run live.
You can try like this:
#include <iomanip>
#include <iostream>
int main()
{
double a = 1.267432;
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(3);
std::cout << a << endl;
return 0;
}
This question already has answers here:
How can I change the precision of printing with the stl?
(2 answers)
Closed 7 years ago.
If I have stored a value in a double variable like
double d = pow(...)
and this function, let's say, evaluates to an integer, and if I print it using cout, it prints only the integer without trailing zeroes.
How can I print the trailing zeroes as well?
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
return 0;
}
Output:
3.1416
3.14159
3.14159
3.141590000
setprecision(int n) sets the specified precision
You are probably looking for std::setprecision. Something like this:
std::cout << std::setprecision (10) << d << std::endl;
Suppose i do the following:
Example A:
float i = 123.0f;
std::cout << i;
I'll get the output 123
My question is when the default precision of float is 6 for std::cout why do I get the output 123 ?
You want std::fixed to show these insignificant zeroes. By default, extra zeroes are cropped for readability.
double f = 3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
Outputs:
3.1416
3.14159
3.14159
3.141590000