What is the use of fixed keyword in setprecision while dealing with formatting of numbers in C++? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of What is the use of fixed keyword in setprecision while dealing with formatting of numbers in C++? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have seen a common programming practice to use fixed while using setprecision. Just wanted to know why it is used as I am new to Programming World.

Code in question:

#include <iomanip>
#include <iostream>

int main()
{
    double num1 = 3.12345678;
    std::cout << std::fixed << std::showpoint;
    std::cout << std::setprecision(2);
    std::cout << num1 << std::endl;

    return 0;
}

Answer

It is used to clamp the amount of decimal digits to write.

The setprecision(x) call will limit it to x decimals.

More info here: http://en.cppreference.com/w/cpp/io/manip/setprecision

We are here to answer your question about What is the use of fixed keyword in setprecision while dealing with formatting of numbers in C++? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji