Module: (C++) Variables. Output formats


Problem

5/7

Output Width and Accuracy

Theory Click to read/hide

Advanced Material
Additional output specifiers allow you to control the output of numbers in certain formats.


Minimum field width
For example:
%04d  - the number will be outputed in 4 positions, if the numbers are less than four, then the first will be zeros
 
int a=34; printf("%04d\n",a); //on the screen: 0 0 3 4   

The underscore is specially designed to visually display the output of the number.

%4d – same, only spaces instead of zeros
int a=34; printf("%4d\n",a); // on the screen: _ _ 3 4

Conclusion with a certain accuracy - used to output real numbers. By default, real numbers are displayed with an accuracy of 6 decimal places. But there are cases that need to be deduced with a different accuracy. In this case, it is necessary to indicate how much familiarity to allocate for the number itself and how much after the decimal point.

For example
%9.3f   - the real number will be displayed in 9 positions, with three decimal places.
 
double a=34.24356; 
printf("%9.3f\n",a); // on the screen: _ _ _ 3 2 . 2 4 4
 
Now, let's try it. 

Problem

This is an optional task for those who want to learn more about the C-output operator.

In the sixth line, write an operator that outputs the value of the a-variable in a field 10 characters wide, filled with zeros on the left (right justification)