std::fixed, std::scientific, std::hexfloat, std::defaultfloat in C++
A collection of manipulators to regulate the formatting of output streams are provided by the <iomanip> header in C++. Std::fixed, std::scientific, std::hexfloat, and std::defaultfloat are four often used manipulators for controlling the presentation of floating-point numbers. The std::fixed manipulator is applied to a stream that requires the display of floating-point numbers in fixed-point notation to ensure a consistent amount of decimal places. On the other hand, the std::scientific manipulator expresses numerical values in scientific notation by multiplying them by a power of ten. Displaying floating-point numbers in hexadecimal format is possible with the use of the std::hexfloat manipulator, which is a helpful feature for some apps. The std::defaultfloat manipulator, which undoes the previous std::fixed, std::scientific, or std::hexfloat manipulators, is the last one to restore the floating-point formatting to its default mode. Programmers using these manipulators to meet their particular requirements can easily customize the representation of floating-point values in output streams.
Methods used are:
- std::fixed:
Method: The insertion operator (<<) is used to apply the std::fixed manipulator to a stream.
Its goal is to specify that fixed-point notation should be used when displaying floating-point numbers.
std::cout << std::fixed << 123.456789 << std::endl;
- std::scientific:
Method: The insertion operator is used to apply std::scientific, just like it does for std::fixed.
Its purpose is to tell the stream to show scientific notation for floating-point numbers.
std::cout<<std::scientific<<123.456789<<std::endl;
- std::hexfloat:
Method: The insertion operator is used to apply the std::hexfloat specification, which controls how floating-point numbers are shown in hexadecimal format.
It is very helpful for handling floating-point data represented in hexadecimal.
std::cout << std::hexfloat << 123.456789 << std::endl;
- std::defaultfloat:
Method: The insertion operator is used to apply std::defaultfloat, just like the others.
Its goal is to restore the formatting to its default state and undo the effects of any previous manipulators, including std::fixed, std::scientific, and std::hexfloat.
std::cout << std::fixed << 123.456789 << std::endl;
std::cout << std::defaultfloat << 123.456789 << std::endl;
C++ Code:
std::fixed:
Let us take an example to illustrate the std::fixed in C++.
#include <iostream>
#include <iomanip>
int main() {
double number = 123.456789;
std::cout << std::fixed << number << std::endl;
return 0;
}
Output:
123.456789
std::scientific:
Let us take an example to illustrate the std::scientific in C++.
#include <iostream>
#include <iomanip>
int main() {
double number = 123.456789;
std::cout << std::scientific << number << std::endl;
return 0;
}
Output:
1.234568e+02
std::hexfloat:
Let us take an example to illustrate the std::hexfloat in C++.
#include <iostream>
#include <iomanip>
int main() {
double number = 123.456789;
std::cout << std::hexfloat << number << std::endl;
return 0;
}
Output:
0x1.edd3c0c5588ccp+6
std::defaultfloat:
Let us take an example to illustrate the std::defaultfloat in C++.
#include <iostream>
#include <iomanip>
int main() {
double number = 123.456789;
std::cout << std::fixed << number << std::endl;
std::cout << std::defaultfloat << number << std::endl;
return 0;
}
Output:
123.456789
123.457
Example 2:
#include <iostream>
#include <iomanip>
int main() {
double number = 123.456789;
// Using std::fixed
std::cout << "Using std::fixed: " << std::fixed << number << std::endl;
// Using std::scientific
std::cout << "Using std::scientific: " << std::scientific << number << std::endl;
// Using std::hexfloat
std::cout << "Using std::hexfloat: " << std::hexfloat << number << std::endl;
// Using std::defaultfloat
std::cout << "Using std::defaultfloat: " << std::defaultfloat << number << std::endl;
return 0;
}
Output:
Using std::fixed: 123.456789
Using std::scientific: 1.234568e+02
Using std::hexfloat: 0x1.edd3c07ee0b0bp+6
Using std::defaultfloat: 123.457
Example 3:
#include <iostream>
#include <iomanip>
int main() {
double number = 123.456789;
// Displaying the number in default format
std::cout << "Default format: " << number << std::endl;
// Displaying the number in fixed-point notation
std::cout << "Fixed-point notation: " << std::fixed << number << std::endl;
// Displaying the number in scientific notation
std::cout << "Scientific notation: " << std::scientific << number << std::endl;
// Displaying the number in hexadecimal format
std::cout << "Hexadecimal format: " << std::hexfloat << number << std::endl;
// Reverting to default format
std::cout << "Back to default format: " << std::defaultfloat << number << std::endl;
return 0;
}
Output:
Default format: 123.457
Fixed-point notation: 123.456789
Scientific notation: 1.234568e+02
Hexadecimal format: 0x1.edd3c07ee0b0bp+6
Back to default format: 123.457
Conclusion:
Programmers can modify the formatting of floating-point numbers in output streams with great strength by using C++ manipulators, especially std::fixed, std::scientific, std::hexfloat, and std::defaultfloat from the <iomanip> header. You can choose between displaying numbers in fixed-point notation, scientific notation, hexadecimal format, or the default style with these manipulators. This control is helpful for adjusting the output to match particular needs, making the code easier to comprehend, and promoting communication between various users or systems. Gaining knowledge of and using these manipulators improves the accuracy and readability of output from C++ programs, increasing the language's adaptability for a range of uses.