Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java printf

// printf formats the text by placing the placeholders into the first string
// placeholders are noted first with '%s' and then you list them in their order
System.out.printf( "Test ( %s ): %s", 3.75, "ok" );
// will give: 
Test ( 3.75 ): ok
// comlete placeholders you use for known variables:
// %f : float and double
// %d : int and long
// %c : char
// %b : boolean
float f = 15.47f;
long l = 765432L;
char c = '@';
boolean b = true;
System.out.printf( "Test2 ( %f ): %d -> %c%b", f, l, c, b );
// will give: 
Test2 ( 15,470000 ): 765432 -> @true
// number of decimals:
// %.4f : 4 decimals after the dot
System.out.printf( "Test3 ( %.4f ): %d -> %c%b", f, l, c, b );
// will give:
Test3 ( 15,4700 ): 765432 -> @true
Comment

How to Use the printf() Function in Java

public class Main{
    public static void main(String[] args) {
        double value = 2.3897;
        System.out.println(value);
        System.out.printf("%.2f" , value);
    }
}



/* Here, I am declaring a double type variable named value
and I am assigning 2.3897 to it.
Now when I use the println() function,
it prints the whole value with the 4 digits after the radix
point.

This is the output:
*/


// 2.3897
// 2.39
Comment

How to Use the printf() Function in Java

/* This printf() function works as a formatted print function.
Think about the two scenarios given below:

Scenario 1: Your friend Tommy wants you to provide him your
notebook's PDF via an email. You can simply compose an email,
provide a subject as you like (such as, hey Tommy,
it's Fahim). You can also avoid writing anything to the body
part of the email and send that email after attaching the PDF
with the email. As simple as that – you don't need to maintain
any courtesy with your friend, right?

Scenario 2: You couldn't come to your class yesterday.
Your professor asked you to provide the valid reasons with
proof and submit the documents via an email.

Here, you can't mail your professor like you did for you
friend, Tommy. In this case, you need to maintain formality
and proper etiquette. You have to provide a formal and legit
subject and write the necessary information in the body part.
Last but not least, you have to attach your medical records
with your email after renaming them with the proper naming
convention. Here, you formatted your email as the authority
wants!

In the printf()function, we follow the second scenario.
If we want to specify any specific printing format/style,
we use the printf()function.

Let me give you a short example of how this works:
*/



public class Main{
    public static void main(String[] args) {
        double value = 2.3897;
        System.out.println(value);
        System.out.printf("%.2f" , value);
    }
}



/* Here, I am declaring a double type variable named value
and I am assigning 2.3897 to it.
Now when I use the println() function,
it prints the whole value with the 4 digits after the radix
point.

This is the output:
*/


// 2.3897
// 2.39


/* But after that, when I am using the printf() function,
I can modify the output stream of how I want the function to
print the value. Here, I am telling the function that I want
exactly 2 digits to be printed after the radix point.
So the function prints the rounded value up to 2 digits after
the radix point.

In this type of scenario, we normally use the printf()
function. But keep in mind that it has a wide variety of
uses in the Java programming language.
*/
Comment

PREVIOUS NEXT
Code Example
Java :: function overriding java 
Java :: java initialize class 
Java :: press enter in robot java 
Java :: assert log in unit testing 
Java :: java remove map 
Java :: check each character in a string java 
Java :: file java class 
Java :: how to sum a 2d array in java 
Java :: how to create a gui in java 
Java :: spring boot dockerfile 
Java :: java search string in string 
Java :: remove last character from stringbuffer 
Java :: jmeter get var 
Java :: JsonArray get first Object 
Java :: arraylist index java 
Java :: copying primitive array to arraylist in java 
Java :: java actionperformed 
Java :: breadth first search bst java 
Java :: primefaces download file 
Java :: even and odd java & 
Java :: java super keyword 
Java :: java compute sum and average of array elements 
Java :: array find max in java 
Java :: add int to list java 
Java :: print anything in java 
Java :: pass array to method java 
Java :: java loops 
Java :: check if char is letter or digit 
Java :: java.lang.NoClassDefFoundError: 
Java :: how to change resource color to int color in android 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =