PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How can I print a a number upto 6 decimal digits in c++?

 August 11, 2022     c++, decimal, double, precision     No comments   

Issue

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;
}

Solution

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


Answered By - BlueTune
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing