This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 1 year ago.
I want to convert 3 to 3.00. I have done explicit type conversion. but guess it doesn't seem to work!whats wrong?
my code:
#include<iostream>
using namespace std;
int main(){
int PI = 3;
double a = static_cast<double>(PI);
cout<<a;
}
output: 3
This may help.
#include<iostream>
using namespace std;
int main(){
int PI = 3;
double a = static_cast<double>(PI);
cout.precision(2);
cout << std::fixed << a;
}
Related
This question already has answers here:
Fixed Decimal Precision [duplicate]
(4 answers)
Closed 2 years ago.
I am trying a problem. I need to print the ans in 6 decimal digits. For example if the ans is 64, I want to print 64.000000
I tried the following way.
what I did wrong?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
float n;
while(t--)
{
cin>>n;
float s=(n-2)*180.000000;
float w=(s/n)*1.000000;
cout<<w*1.000000<<setprecision(6)<<endl;
}
return 0;
}
You can make use of std::fixed:
#include <iostream>
#include <iomanip>
void MyPrint(int i)
{
double d = static_cast<double>(i);
std::cout << std::setprecision(6) << std::fixed << d << std::endl;
}
int main() {
MyPrint(64);
MyPrint(100);
return 0;
}
Running the above code online results in the following output:
64.000000
100.000000
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 3 years ago.
I have this code and the function pow() is behaving differently in two cases.
For "p" I'm using the number 3, in the first output is showing 124 but the second shows 125.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
long long int p, ans;
cin >> p; //3
ans = pow(5,p);
cout << ans << endl; //124
cout << pow(5,p) << endl; //125
return 0;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers?
user#box:~/c/precision$ cat precision.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 10, b = 3;
float ans = (a/b);
cout<<fixed<<setprecision(3);
cout << (a/b) << endl;
cout << ans << endl;
return 0;
}
user#box:~/c/precision$ g++ -o precision precision.cpp
user#box:~/c/precision$ ./precision
3
3.000
Cast the operands to floats:
float ans = (float)a / (float)b;
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers?
user#box:~/c/precision$ cat precision.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 10, b = 3;
float ans = (a/b);
cout<<fixed<<setprecision(3);
cout << (a/b) << endl;
cout << ans << endl;
return 0;
}
user#box:~/c/precision$ g++ -o precision precision.cpp
user#box:~/c/precision$ ./precision
3
3.000
Cast the operands to floats:
float ans = (float)a / (float)b;