How can we display floats in cpp without including <cstdio>? I tried by cout but it won't display decimal digits after "0". | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 */

20th May 2020, 4:21 AM
Aditya Raj
Aditya Raj - avatar
17 Answers
+ 1
I think you can just type fixed<<setprecision(20)
20th May 2020, 4:31 AM
ycsvenom
ycsvenom - avatar
+ 1
What do you mean? Do you mean 2.7............?
20th May 2020, 6:32 AM
ycsvenom
ycsvenom - avatar
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
20th May 2020, 4:45 AM
Aditya Raj
Aditya Raj - avatar
0
How much do you want?
20th May 2020, 4:49 AM
ycsvenom
ycsvenom - avatar
0
And do you want to fill it with zeros?
20th May 2020, 4:49 AM
ycsvenom
ycsvenom - avatar
0
Only 6 digits were needed. But what if we want all digits for double e
20th May 2020, 4:51 AM
Aditya Raj
Aditya Raj - avatar
0
YCS-Venom I meant 14049.30493000
20th May 2020, 11:31 AM
Aditya Raj
Aditya Raj - avatar
0
just type like this double x=14049.30493000 ; cout<<fixed<<setprecision(8)<<x; Or you can type it directly
20th May 2020, 11:36 AM
ycsvenom
ycsvenom - avatar
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.
20th May 2020, 11:41 AM
Aditya Raj
Aditya Raj - avatar
0
Is it code couch?
20th May 2020, 11:52 AM
ycsvenom
ycsvenom - avatar
0
YCS-Venom No, it was some random question on hackerrank.
20th May 2020, 11:54 AM
Aditya Raj
Aditya Raj - avatar
0
Can you give me the link?
20th May 2020, 11:55 AM
ycsvenom
ycsvenom - avatar
0
YCS-Venom It was one of the easy ones. Just trying without using printf Link: http://hr.gs/4ljk3
20th May 2020, 11:58 AM
Aditya Raj
Aditya Raj - avatar
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; }
20th May 2020, 12:12 PM
ycsvenom
ycsvenom - avatar
0
YCS-Venom Yeah, it works.
20th May 2020, 12:19 PM
Aditya Raj
Aditya Raj - avatar
0
H+3=
21st May 2020, 3:31 AM
James Sever
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.
21st May 2020, 10:33 AM
ALI Mamdouh Ahmad
ALI Mamdouh Ahmad - avatar