Skip to content
Related Articles

Related Articles

std::fixed, std::scientific, std::hexfloat, std::defaultfloat in C++
  • Difficulty Level : Hard
  • Last Updated : 06 Jul, 2017

Formatting in the standard C++ libraries is done through the use of manipulators, special variables or objects that are placed on the output stream. There are two types of floating point manipulators namely, fixed floating point and scientific floating point. These all are defined in header <iostream>.

    Use of precision : In the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display both before and after the decimal point, while in both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if they are trailing decimal zeros.
  1. std::fixed – Fixed Floating-point notation : It write floating-point values in fixed-point notation. The value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
  2. std::scientific – Scientific floating-point notation : It writes floating-point values in Scientific-point notation. The value is represented always with only one digit before the decimal point, followed by the decimal point and as many decimal digits as the precision field (precision). Finally, this notation always includes an exponential part consisting on the letter “e” followed by an optional sign and three exponential digits.
  3. std::hexfloat – Hexfloat floating-point notation : It outputs the desired number after the conversion into hexadecimal format after the precision of no. is initialized ( as discussed in prev. cases ).
  4. std::defaultfloat – defaultfloat floating-point notation : It outputs the desired number same as default after the precision of no. is initialized ( as discussed in prev. cases ). It is mainly used to distinguish among other used formats for understandability of code.

  5. // C++ code to demonstrate the 
    // working of 
    // std::fixed
    // std::scientific
    // std::hexfloat
    // std::defaultfloat
    #include <iostream> 
    using namespace std;
    int main()
        // Initializing floating point variable
        double a = 4.223234232;
        double b = 2323.0;
        // Specifying precision
        // Printing normal values
        cout << "Normal values of floating point numbers\na = ";
        cout << a << "\nb = " << b << '\n' ;
        // Printing values using fixed ( till 4 )
        cout << "Values using fixed \n" << std::fixed;
        cout << a << "\n" << b << '\n'
        // Printing values using scientific ( till 4 )
        // after 4, exponent is used
        cout << "Values using scientific are : " << std::scientific << endl;
        cout << a << '\n' << b << '\n'
        // Printing values using hexfloat ( till 4 )
        cout << "Values using hexfloat are : " << std::hexfloat << endl;
        cout << a << '\n' << b << '\n'
        // Printing values using defaultfloat ( till 4 )
        // same as normal
        cout << "Values using defaultfloat are : " << std::defaultfloat << endl;
        cout << a << '\n' << b << '\n'
        return 0;



    Normal values of floating point numbers
    a = 4.223
    b = 2323
    Values using fixed 
    Values using scientific are : 
    Values using hexfloat are : 
    Values using defaultfloat are : 

    This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

    My Personal Notes arrow_drop_up
Recommended Articles
Page :