Using the iomanip C++ library
Provides capabilities for handling output formatting
The
It has the following functions:
setiosflags
- Defines flags formatresetiosflags
- Resets flags formatsetbase
- Sets the base of the flagssetfill
- Defines the filling of characterssetprecision
- Sets the precision of decimalssetw
- Sets the width of a fieldget_money
- Gets the monetary valueput_money
- Insert the monetary valueget_time
- Get the time and dateput_time
- Get the time and date
Some examples
setfill
and setw
Fill with a certain character on the left side of the number(int, float, double,…)
Filled with 6 blank spaces(10 minus the number of characters), so the output will be:
3.69
. If we changestd::setfill (' ')
tostd::setfill ('-')
the output will be: ——3.69 .
setprecision
Defines how many digits you want to appear if there is no more after the comma ( in this case, point .
), it will fill with 0
zeros if we use std::fixed
.
Output:
3.14
Output:
3.142
because the next significant number is equal to or greater than 5, so it approximates the previous one.
And if you change it to std::setprecision( 9 )
, the output will be: 3.14159
will not complete with zeros, but using std::fixed
The output will be:
3.141590000
get_money
and put_money
Informs the value in integer, in other words transforms: float
, double
,… in integer. They are useful for use with data entry. Example:
get_time
Formats the time entry, example:
Output:
Comments