+ 2
How can we display floats in cpp without including <cstdio>? I tried by cout but it won't display decimal digits after "0".
//used setprecision() for required digits #include <iostream> #include<iomanip> using namespace std; int main() { int a; long int b; char c; float d; double e; cin>>a>>b>>c>>d>>e; cout<<setprecision(20)<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e; return 0; } /* sample i/p:- 3 1354257897655556 a 334.2304 14049.304930000 */
18 Respuestas
+ 1
I think you can just type
fixed<<setprecision(20)
+ 1
What do you mean?
Do you mean 2.7............?
0
YCS-Venom
Thanks for reply. Works fine upto 6 decimal digits.
"cout<<fixed<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e;" like this
0
How much do you want?
0
And do you want to fill it with zeros?
0
Only 6 digits were needed. But what if we want all digits for double e
0
YCS-Venom
I meant 14049.30493000
0
just type like this
double x=14049.30493000 ;
cout<<fixed<<setprecision(8)<<x;
Or you can type it directly
0
YCS-Venom
Yeah, I get that. But there are multiple test cases. I can't initialise any of them. Without using "setprecision ()" or "fixed" 5 out of 9 were wrong but I used fixed and it worked. So, thanks a lot.
0
Is it code couch?
0
YCS-Venom
No, it was some random question on hackerrank.
0
Can you give me the link?
0
YCS-Venom
It was one of the easy ones. Just trying without using printf
Link: http://hr.gs/4ljk3
0
Aditya Raj
That's work fine
int main() {
int a;
long int b;
char c;
float d;
double e;
cin>>a>>b>>c>>d>>e;
cout<<a<<endl<<b<<endl<<c<<endl<<fixed<<d<<endl<<e;
return 0;
}
0
YCS-Venom
Yeah, it works.
0
H+3=
0
You should use casting just like this:
Cout<<float(x+y);
As if one of the variables x, y or both of them is float , the result will be float. Even if the two variables were integers the result will either be float.