+ 5

Hello everyone!!

I know there's a method or smth like that in C++ that is useful when we want to output a value with a specific number of decimals. It was like" setprecizion" or something but I forgot how to use it. Can anyone help me? Thanks a lot

6th Feb 2018, 3:00 PM
Antonia Avadanei
Antonia Avadanei - avatar
19 ответов
+ 2
https://code.sololearn.com/cZp5uO44ZEvu/?ref=app I edited your code @Antonia. Does this help?
13th Feb 2018, 2:18 PM
Zeke Williams
Zeke Williams - avatar
+ 5
#include <iostream> #include <iomanip> using namespace std; int main () { double f =3.14159; cout << setprecision(5) << f << '\n'; cout << setprecision(9) << f << '\n'; cout << fixed; cout << setprecision(5) << f << '\n'; cout << setprecision(9) << f << '\n'; return 0; } // OUTPUT 3.1416 3.14159 3.14159 3.141590000 // Methods ios_base::precision = Get/Set floating-point decimal precision (public member function ) fixed = Use fixed floating-point notation (function ) Hope this helps.
6th Feb 2018, 3:14 PM
Al Z
Al Z - avatar
+ 3
cout << fixed << setprecision (4); //now anything outputted after this will have //4 decimal places
6th Feb 2018, 3:06 PM
Zeke Williams
Zeke Williams - avatar
+ 3
Yes! I Got it! Thanks a lot!!
6th Feb 2018, 3:17 PM
Antonia Avadanei
Antonia Avadanei - avatar
+ 2
Thank you!!
6th Feb 2018, 3:13 PM
Antonia Avadanei
Antonia Avadanei - avatar
+ 2
you can simple include for example as 0.9f or 0.9d where f includes the floating point number and d includes the double point numbers. you need not set the precision over there simple in the cout that is in the output stream simply write it using the cout<<0.9f or cout<<0.9d where float takes up to 7 decimal places where double takes upto 38 decimal places A best example is #include<iostream> using namespace std; int main() { float f=10.235; double d=1023.12456789; cout<<0.2f; cout<<"\n"<<0.9d; return 0; } sorry the code is not in proper citation
13th Feb 2018, 12:05 PM
shanker
+ 2
I'm not that sure but the first ones actually if uh don't use 0.f it would result into a double point so if uh use f it takes a floating or else it is considered as default by the compiler
13th Feb 2018, 12:53 PM
shanker
+ 2
I am so sorry that was the one suitable in c language not in c++ I'm so sorry @Antonia
13th Feb 2018, 1:50 PM
shanker
+ 2
It's so simple add all the float or double point numbers and store it in a result now set the precision to result
13th Feb 2018, 1:59 PM
shanker
+ 2
thanks @Zeke Williams I understood your code. it s easy now. @shanker this works if I eliminate the "0.2" and "0.9" in front of f and d variables.
13th Feb 2018, 2:28 PM
Antonia Avadanei
Antonia Avadanei - avatar
+ 1
Did you get it now !!!! 😅😁
13th Feb 2018, 12:58 PM
shanker
+ 1
Can I know what do uh want exactly for !
13th Feb 2018, 1:03 PM
shanker
+ 1
@shanker I wanted to know exactly the output for your own example. My problem was to calculate the arithmetic sum of some given numbers and display that value with a specific number of decimal places.
13th Feb 2018, 1:48 PM
Antonia Avadanei
Antonia Avadanei - avatar
+ 1
Bro it wont work because it is printing the .2 and .9 of the float and double values
13th Feb 2018, 2:20 PM
shanker
+ 1
It's ok @shanker 😂😂 Thank you for your interest anyway
13th Feb 2018, 2:24 PM
Antonia Avadanei
Antonia Avadanei - avatar
+ 1
It's just. Because of uh 👻 just kidding only about the program !!! 🙈
13th Feb 2018, 2:25 PM
shanker
+ 1
I told it's wrong when they r included and uh r saying when they are removed both r the same 😅😁
13th Feb 2018, 2:29 PM
shanker
0
@shanker and what would be the output in your example? I'm afraid I didn't understand your explanation. Why did you use 0.2f instead of 0.9f? I tried your example and it didn't work, so what am I doing wrong? https://code.sololearn.com/cVu4H4Z6UJ0q/?ref=app
13th Feb 2018, 12:52 PM
Antonia Avadanei
Antonia Avadanei - avatar
0
@shanker not exactly.. sorry 😇😇 could you please write your own code and post it here?
13th Feb 2018, 1:02 PM
Antonia Avadanei
Antonia Avadanei - avatar