Can I set variables to 1 decimal place? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can I set variables to 1 decimal place?

I am tinkering again and I have found some code online: cout << fixed << setprecision(1)<< MyFloat; but this only applies to that one instance at that one time. Is there any way I can set a default in the programme so it knows to always use 1dp with these variables? Thank you

26th Jul 2017, 12:40 AM
Richard Appleton
Richard Appleton - avatar
4 Answers
+ 4
Simply start your code with this : cout.setf(ios::fixed); cout.precision(1); //Now its the default precision, one digit after the decimal, always, wont count significant digits... // General - stream_name.setf(ios::ios_flag_to_use); stream_name.precision(default_precision_value);
27th Jul 2017, 12:56 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 9
Yeah, if you only need 1dp precision, just store the value as 1dp to your variable, instead of manipulating output for each time.
26th Jul 2017, 5:33 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Thank you. that is great.
27th Jul 2017, 12:58 AM
Richard Appleton
Richard Appleton - avatar