Javatpoint Logo
Javatpoint Logo

C++ Program to Demonstrate use of Formatting Flags on Float Output

In this article, we will discuss the C++ program to demonstrate the use of formatting flags on float output.

The float output can be formatted with the aid of the formatting flags included in the ios_base header. The output format for the float can be set to default, scientific, or fixed. The default format contains the same number of significant digits as the original float value, whereas the fixed format includes a defined number of significant digits.

The library provides a collection of C++ formatting tools, including those for floating-point values. Handling the appearance of floating-point values in the output stream is made easier with the help of these tools. A description of the formatting flags utilized in the sample program can be found here:

  1. fixed: By using this flag, a fixed-point notation will be displayed instead of floating-point numbers in the numerical output. It guarantees that the output will be presented with a set amount of digits following the decimal point.
  2. scientific: By setting this flag, floating-point numerical output in scientific notation is presented. It makes sure the output is shown as an x 10^n and is helpful when working with very large or very small values.
  3. showpoint: If there are no digits following the decimal point, this flag makes sure that the decimal point is always shown in the output.
  4. setprecision (n): Set the decimal precision to be utilized when formatting floating-point values with the setprecision(n) function. The number of digits that will be shown following the decimal point is specified.

Example:

Let us take an example to demonstrate the use of formatting flags on float output in C++.

Output:

Fixed-point notation: 1234.57
Scientific notation: 1.23e+03
Show point and trailing zeros: 1.23e+03
No trailing zeros: 1.23e+03

Explanation:

In this program, the number 1234.56789 is a float variable. The program using formatting flags controls the way this number is shown in the output. The following formatting options are shown by the program:

  • fixed: It shows the value with two decimal places in fixed-point notation.
  • scientific: It shows the number to two decimal places in scientific notation.
  • showpoint: It makes sure that trailing zeros and the decimal point are always visible.
  • noshowpoint: It turns off the trailing zeros display.

1. Include Directives:

The standard input-output stream library is included with #include <iostream>, enabling you to operate on input and output on the console.

2. #include <iomanip>:

It includes the library for input-output manipulation, which offers facilities for formatting program output.

3. Namespace Declaration:

using namespace std;: This line eliminates the requirement to prefix entities from the standard C++ library with std::, enabling the program to utilize them.

4. main Function:

int main(): It is the primary function that initiates program execution.

5. Float Initialization:

float number = 1234.56789;: In this line, the value 1234.56789 is assigned to the float variable number.

6. Formatting for Output:

The application shows several formatting flags, including:

fixed with setprecision(2): It shows the value to two decimal places in fixed-point notation.

scientific with setprecision(2): It provides the value with two decimal places in scientific notation.

showpoint: It ensures that the decimal point is always visible together with any trailing zeros.

noshowpoint: It turns off the trailing zero display.

7. Output Statements:

The prepared output and descriptive strings are printed to the console using the cout statements.

8. Return Statement:

return 0;: The operating system receives a message indicating that the program has properly executed and returned 0.

Conclusion:

In conclusion, it is possible to precisely control how floating-point numbers are presented in your C++ programs by knowing and using these formatting options. This degree of control is useful in settings like scientific computing, financial applications, or data analysis jobs, where exact formatting and presentation of numerical data are essential.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA