Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,211 members, 7,818,717 topics. Date: Sunday, 05 May 2024 at 10:40 PM

Java | Formatting Computer Console Output - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java | Formatting Computer Console Output (840 Views)

What Is The Highest Number Output By This Function... / Formatting Toolbar (2) (3) (4)

(1) (Reply)

Java | Formatting Computer Console Output by Nobody: 10:00am On Nov 05, 2015
I started from the beginning to tutor Java in my blog - http://www.chinazomidoko..com

THIS IS JUST ONE OF MY POSTS.

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 usinf 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);
}
}


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

Peace!!

(1) (Reply)

List of useful Android Codes / I Can Help You / Get You Website Project Done At Affordable Cost

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.