C++ Program to Demonstrate use of Formatting Flags on Float Output29 Aug 2024 | 4 min read 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:
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:
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. |
The std::allocator_arg is a struct in C++ that is mostly used in conjunction with allocators to add another layer of indirection when building objects with a particular allocator. It frequently works with the std::allocator class. Specifically added in C++11, std::allocator_arg is a member of the C++...
4 min read
In this article, you will learn about the std::mt19937 class in C++ with its syntax, parameters, and examples. In C, we use the functions such as rand() and srand(), while in C++, we use std::rand() and std::srand(). Numerous more advanced random number generators are available to align...
4 min read
In C++, Object-oriented programming (OOP) is a model of computer programming that uses classes and objects to structure the code. It facilitates modularity, reusability, and scalability. C++ is one of the most widely used OOP languages and supports some of the concepts of OOP, like...
7 min read
Object-oriented programming (OOP) is a powerful paradigm for developers that allows them to model real-world things and interactions in their code. Creating and interacting with objects is critical in C++, one of the most popular programming languages. In this post, we'll look at the process of...
4 min read
Actual and formal arguments in C++ refer to the values passed and received by a function, respectively. A function definition specifies the number, types, and names of its formal arguments, while a function call provides the corresponding actual arguments. The process of matching actual arguments with...
3 min read
Before diving straight into our topic, , let us understand what exceptions and exception handling are. Exceptions are the errors which occur while we are programming and are generally treated as unwanted errors or to understand better. They are like the hurdles caused while programming, and...
3 min read
In C++, the keyword static is used to give element unique properties. Static elements are only allocated storage in the static storage area once during the program's lifetime. And they are valid for the duration of the programme. The following are examples of static keywords: Functions with...
3 min read
partition point() acquires the partition point: Returns an iterator to the first partitioned element in the range [first, last] for which pred(predicate) is false, indicating the partition point of that element. As if partition had been called with the identical inputs, the range's elements must already be...
4 min read
Various streams are available in the C++ standard library for handling input and output activities. One of these streams is called cerr, which is short for "standard error". Cerr is made specifically for error messages and diagnostics, unlike the cout stream, which is used for general...
3 min read
Introduction: C++ is a powerful programming language that provides many features to developers to create efficient and robust applications. One of the essential features of C++ is its Standard Template Library (STL), which offers many data structures and algorithms to make programming easier and more efficient. Sequence Containers...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India