+ 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
19 ответов
+ 2
https://code.sololearn.com/cZp5uO44ZEvu/?ref=app
I edited your code @Antonia. Does this help?
+ 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.
+ 3
cout << fixed << setprecision (4);
//now anything outputted after this will have
//4 decimal places
+ 3
Yes! I Got it! Thanks a lot!!
+ 2
Thank you!!
+ 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
+ 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
+ 2
I am so sorry that was the one suitable in c language not in c++ I'm so sorry @Antonia
+ 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
+ 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.
+ 1
Did you get it now !!!! 😅😁
+ 1
Can I know what do uh want exactly for !
+ 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.
+ 1
Bro it wont work because it is printing the .2 and .9 of the float and double values
+ 1
It's ok @shanker 😂😂 Thank you for your interest anyway
+ 1
It's just. Because of uh 👻 just kidding only about the program !!! 🙈
+ 1
I told it's wrong when they r included and uh r saying when they are removed both r the same 😅😁
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
0
@shanker not exactly.. sorry 😇😇 could you please write your own code and post it here?