0

I am trying a problem. I need to print the ans in 6 decimal digits. For example if the ans is 64, I want to print 64.000000 I tried the following way. what I did wrong?

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    long long t;
    cin>>t;
    float n;
    while(t--)
    {
        cin>>n;
        float s=(n-2)*180.000000;
        float w=(s/n)*1.000000;
        cout<<w*1.000000<<setprecision(6)<<endl;
    }

    return 0;
}
Red Mist
  • 3
  • 1
  • 3
  • You want to use `std::fixed`. https://en.cppreference.com/w/cpp/io/manip/fixed – JohnFilleau Mar 14 '20 at 17:02
  • The shown code demonstrates that you are familiar with I/O formatting manipulators, like `std::setprecision`. This is not the only I/O manipulator that's available to you. Are you familiar with all the others? If not, you will find more information in your C++ book. – Sam Varshavchik Mar 14 '20 at 17:02
  • Unrelated, but please [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Mar 14 '20 at 17:03
  • @Someprogrammerdude why? :/ – Red Mist Mar 14 '20 at 17:33

1 Answers1

1

You can make use of std::fixed:

#include <iostream> 
#include <iomanip>

void MyPrint(int i)
{
    double d = static_cast<double>(i);
    std::cout << std::setprecision(6) << std::fixed << d << std::endl;

}

int main() {

    MyPrint(64);
    MyPrint(100);

    return 0;
}

Running the above code online results in the following output:

64.000000
100.000000
BlueTune
  • 1,023
  • 6
  • 18
  • Hi. Thank you so much for your response. but here's is the thing, I want to print 6 digits after the decimal point for every value. If I input 100 in your code, if prints upto 5 digits after decimal, 4 digits if I input 1000. https://pastebin.com/EWZufxUY here is the code. I want to print 64.000000 for 64, 100.000000 for 100 and 1000.000000 for 1000 – Red Mist Mar 14 '20 at 17:30
  • @Tahmid: I updated my answer on the base of your comment. – BlueTune Mar 14 '20 at 17:54