Module: (Java) Variables. Output formats


Problem

5/7

Output Width and Accuracy

Theory Click to read/hide

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

Minimum field width
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; System.out.printf("%04d",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; System.out.printf(“%4d”,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.

Example,
%9.3f   - the real number will be displayed in 9 positions, with three decimal places.

double a=34.24356; System.out.printf("%9.3f",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 Java-output operator.

In the 4th 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)