Thursday 5 November 2015

Java | Formatting Computer Console Output



There are situations where by you want to have just 4 decimal places or 2 decimal places whatever the case and 

you know quite fully well that making use of double in your computation cannot just give you a 2 decimal or 4 decimal places value.

There are two ways to trick the computer into giving you what you want.


The first is by multiplying the numerator by 100, casting the answer into an integer and then dividing the new numerator by 100.0 to obtain a 2 decimal place value.

Chunk of code:

double number = 2.0 / 3;
double answer = (int)(number * 100) / 100.0;
System.out.println("number is "+answer);

This results to a 2 decimal place value.
If you want to obtain a 3 decimal place value, you make use of 1000.
If you want to obtain a 4 decimal place value. you make use of 10000.

The second way to handle this is by the use of formats (printf).
In this way, we format the output using printf method instead of print or println.
Therefore, it becomes System.out.printf

A format specifier specifies how an item should be displayed.
A format may be a numeric value, boolean value, character or string.
A simple specifier consists of a percent sign (%) followed by a conversion code.

Examples of specifiers are:

Specifier         Output               Example
%b                a boolean value     true or false
%c                a character           'k'
%d                an integer            20
%f                a floating point      53.89882343894
%e                a number in          4.322234989e+02
                  standard specific
                  number

%s                a string              "My name is Idoko Nicholas Chinazom"



Let's look at each of these specifiers individually

%7b   -  This specifier adds 2 spaces before false and 3 spaces before true.

%2c   -  This specifier adds one space before the character.

%7d   -  This specifier signifies that the width of the integer is 7. Therefore, if the width of the integer is less than 7 add spaces but if the width of the integer is greater than 7 the width adjusts and automatically increases.

%8s   -  This specifier operates the same way as %d. It is just that it is for strings while %d is for integers. It says that the width of the string is 8. Therefore, if the width of the string is less than 8 add spaces before the string but if the width of the string is greater than 8 the width adjusts and automatically increases.

%5.2f -  This specifier is for floating point numbers (numbers with fractions or decimal points). This outputs floating point value with a width of 5 including a decimal point and 2 digits after the point. Therefore, there are 2 digits before the decimal point. If the digits before the decimal point is less than 2 add spaces but the if the digits before the decimal point is greater than 2 adjusts and automatically increased.

%7.3e -  This outputs the floating point item with width of 7 including a decimal point, three digits after the decimal point and the exponent part. If the displayed number in scientific notation has width less than 7, add spaces before the number.

Below is a program displaying an example in all the specifiers above.

package nicholas;
public class DisplayFormat{
public static void main (String [] args){
double number = 1.0 / 3;
System.out.printf("For Floating Point: %4.4f", number);
int count = 23;
                System.out.printf("\nFor Integer: %1d", count);
                String name = "Idoko Nicholas Chinazom";
                System.out.printf("\nFor String: %23s", name);
                char symbol = 'c';
                System.out.printf("\nFor Character: %2c", symbol);
                boolean check = true;
                System.out.printf("\nFor Boolean: %5b", check);
                double result = 2 / 9.0;
                System.out.printf("\nFor Standard Scientific Notation: %3.2e\n", result);
}
}


Here is the screenshot of the code:

Here is the output:


Assignment
Write your own program with your own specifiers in printf method.


Peace!!

3 comments:

  1. Plsss. Am a novice. Just wanted to start as a beginner. What do I need to start with..waiting for ur reply.thanks.

    ReplyDelete
  2. I am very sorry for the very late reply but you can start with Java For Dummies.

    ReplyDelete
  3. I am very sorry for the very late reply but you can start with Java For Dummies.

    ReplyDelete