std::fixed, std::scientific, std::hexfloat, std::defaultfloat in C++

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>.
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.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.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 ).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.// 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    cout.precision(4);          // 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;}Output:Normal values of floating point numbers
a = 4.223
b = 2323
Values using fixed 
4.2232
2323.0000
Values using scientific are : 
4.2232e+00
2.3230e+03
Values using hexfloat are : 
0x1.0e49783b72695p+2
0x1.226p+11
Values using defaultfloat are : 
4.223
2323
This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.My Personal Notes
arrow_drop_upSave
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.
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.
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 ).
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.
// 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    cout.precision(4);          // 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;}
Output:
Normal values of floating point numbers
a = 4.223
b = 2323
Values using fixed 
4.2232
2323.0000
Values using scientific are : 
4.2232e+00
2.3230e+03
Values using hexfloat are : 
0x1.0e49783b72695p+2
0x1.226p+11
Values using defaultfloat are : 
4.223
2323

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
cpp-numerics-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Polymorphism in C++
List in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Command line arguments in C/C++
Exception Handling in C++
Sorting a vector in C++
Operators in C / C++
Destructors in C++
Power Function in C/C++
Pure Virtual Functions and Abstract Classes in C++