c++ caring about only 2 digit decimal w/o setprecision [duplicate] - c++

This question already has answers here:
Round double to 3 points decimal [duplicate]
(4 answers)
Closed 3 years ago.
double i = 2.5373737373737....
Is there anyway to get rid of decimals after 2 digits decimals so it can be 2.57? (without displaying it by using setprecision)

setprecision(int) function in iomanip can help to print with required precision.
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
double i =2.537373737;
std::cout << setprecision(3) << i << '\n';
}

Related

How to round off a double number to 2 decimal places in c++ [duplicate]

This question already has answers here:
Printing the correct number of decimal points with cout
(13 answers)
Closed 11 months ago.
I need to write a simple program, which would round off double number to just 2 decimal places. Example:
input - 0.3333333, 124.132001
output - 0.33, 124.13
Use std::set_precision as in
std::cout << std::fixed << std::setprecision(2) << 0.3333333;

"setprecision()" and floating point [duplicate]

This question already has answers here:
Show two digits after decimal point in c++ [duplicate]
(5 answers)
Printing the correct number of decimal points with cout
(13 answers)
Closed 3 years ago.
I'm trying to Run a program that converts Currency From KD to DOLLAR and EURO.
The problem is I'm expecting the output to be with 3 floating numbers but the actual out put is just 1 floating number after the decimal point like the pi.
std::fixed should solve your issue :
double a = 3.149;
std::cout << std::setprecision(6) << std::fixed << a << "\n";
will output :
3.149000

Sin is not giving the correct value [duplicate]

This question already has answers here:
cos returns wrong values?
(3 answers)
Closed 6 years ago.
#include <iostream>
#include <cmath>
Using namespace std;
void main ()
{
double z,a;
cout <<"input a"<<endl;
cin>>a;
z=sin (a);
cout <<"z="<<z <<endl;
system("pause");
}
When I input the variable a with value of 90 it gives me 0.893997
And when I make the variables int or float it gives the same value
The input of 'sin' is in radians (http://en.cppreference.com/w/cpp/numeric/math/sin)
So the answer is, in fact , correct

can't print more decimal of pi [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 7 years ago.
I have tried to use long double type in my program to print out more digits of pi. But it only shows 5 digits decimal.
Here is my code.
int main(int argc, char** argv) {
long double pi_18 = acos(static_cast<long double>(-1));
cout << "pi to 18:" << pi_18 << endl;
return 0;
}
and this is my output:
pi to 18: 3.14159
How can I fix this problem?
Like so:
#include <iomanip>
#include <iostream>
std::cout << std::setw(15) << pi_18 << std::endl;
The width modifier only affects the next formatting operation, so if you want to format multiple numbers, you have to repeat it before every one. Check out the full documentation of format specifiers.
You could use the precision method:
cout.precision(15);
This allows you to define the precision only once. You don't have to repeat it like with std::setw()
For more information see:
http://en.cppreference.com/w/cpp/io/ios_base/precision

C++ Floating point Display [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
floating point precision
when I do cout<<8.0 .Its getting printed as 8.How to Print in the output console of c++ the entire zeros after decimal point like 8.00000000
I tried this cout<<setprecision(5)<<(double)8.0; still printing 8
Use the fixed manipulator
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(6) << (double)8 << "\n";
return 0;
}
http://ideone.com/ShcNIc
See How do I print a double value with full precision using cout?
cout.precision(15);
cout << fixed << 8.0;