C++ Program To Demonstrate Use Of Formatting Flags On Float Output - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Program To Demonstrate Use Of Formatting Flags On Float Output

C++ Program To Demonstrate Use Of Formatting Flags On Float Output

BLUF: Mastering C++ Program To Demonstrate Use Of Formatting Flags On Float Output is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Program To Demonstrate Use Of Formatting Flags On Float Output

C++ is renowned for its efficiency. Learn how C++ Program To Demonstrate Use Of Formatting Flags On Float Output enables low-level control and high-performance computing in the tutorial below.

In this post, we will explore a C++ code example showcasing the application of formatting flags when outputting floating-point numbers.

The float result can be formatted using the formatting flags provided in the ios_base header. You can specify the output format for the float as either default, scientific, or fixed. The default format retains the original float value's significant digits, while the fixed format allows for a specific number of significant digits to be displayed.

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:

  • 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.
  • 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.
  • showpoint: If there are no digits following the decimal point, this flag makes sure that the decimal point is always shown in the output.
  • 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's consider an example to illustrate how to utilize formatting flags for float output in C++.

Example

#include <iostream>
#include <iomanip>
using namespace std;
int main() 
{
 float number = 1234.56789;
 // Fixed-point notation output with two decimal places
 cout << "Fixed-point notation: " << fixed << setprecision(2) << number << endl;
 // Output with two decimal places in scientific notation
 cout << "Scientific notation: " << scientific << setprecision(2) << number << endl;
 // output displaying trailing zeros and with default precision.
 cout << "Show point and trailing zeros: " << showpoint << number << endl;
 // output that displays trailing zeros but has default accuracy.
 cout << "No trailing zeros: " << noshowpoint << number << endl;
 return 0;
}

Output:

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.

  1. #include <iomanip>:

It consists of the library for handling input-output operations, providing features for organizing program output.

  1. Defining Namespace:

By using the "using namespace std;" directive, the need to prefix elements from the standard C++ library with std:: is removed, allowing the program to make use of them.

  1. main Function:

The main function serves as the entry point for program execution.

  1. Initialize Float Variables:

In this statement, the float variable "number" is initialized with the value 1234.56789.

  1. Output Formatting:

The program displays various formatting indicators, such as:

It displays the specified value with a precision of two decimal places using fixed-point notation.

scientific with setprecision(2): It furnishes the numerical value in scientific notation with a precision of two decimal places.

showpoint: It guarantees that the decimal point remains visible along with any following zeros.

noshowpoint: Disabling the display of trailing zeros.

  1. Print Statements:

The generated output and explanatory strings are displayed on the console through the cout statements.

  1. Output of the Return Statement:

return 0;: When the program has completed its task successfully, it sends a signal to the operating system indicating that it has finished executing and returned a value of 0.

Conclusion:

In summary, mastering the formatting options in C++ allows for accurate manipulation of how floating-point numbers are displayed in your programs. This level of precision is valuable in fields such as scientific computing, financial sectors, and data analysis roles, where precise numerical data formatting and display are critical.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience