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 :: how to change custom font to bold italic in java 
Java :: how to add to an arraylist java 
Java :: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection 
Java :: convert java to python 
Java :: jdk 15 download brew 
Java :: spring-boot java header appliacation/json constant 
Java :: do statement java 
Java :: java map create with values 
Java :: hashmaps java 
Java :: Java JTextArea text size 
Java :: implement stack using linked list java 
Java :: how to convert line into string java 
Java :: java list 
Java :: minimum and maximum in array in java 
Java :: Employee Java. 
Java :: latest android sdk version 
Java :: If you are using the git profile, you need to set a Git URI in your configuration. If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration. 
Java :: method java 
Java :: java get enum by ordinal 
Java :: open new fragment from fragment 
Java :: spring xml configuration 
Java :: Example of Creating a Java Stack 
Java :: how to check number of messages in kafka topic 
Java :: java Convert a string IPv4 IP address to the equivalent long numeric value. 
Java :: pop back stack fragment android 
Java :: maven skip make-assembly 
Java :: java benchmark time 
Java :: android studio Toast usage 
Java :: make a textarea not editable javafx 
Java :: recursion of numbers in decending order in java 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =