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 :: java wait for user input 
Java :: java repository sql find not in list 
Java :: string to int in java 
Java :: longest common subsequence of two strings 
Java :: How to draw Bezier Curve in Android 
Java :: insert string in string java 
Java :: final finally finalize 
Java :: polymorphism in oop 
Java :: method resolve file in java 
Java :: one line if statement java 
Java :: how to use arrays java 
Java :: indext of minimum element in an array in java 
Java :: how to scan as a letter in java 
Java :: for java 
Java :: Java loop throug gson JsonElement 
Java :: java bufferedreader read all lines 
Java :: add Duration to date in Kotlin 
Java :: cardview implementation 
Java :: min in java 
Java :: switch case enum java 
Java :: asscending linkedlist remove duplicates valuesjava 
Java :: keytool error: java.lang.Exception: Key pair not generated, alias <demo already exists 
Java :: java php object 
Java :: swing getsource 
Java :: java map create with values 
Java :: check if a char is a space java 
Java :: actuator spring boot 
Java :: how to create xml file in java 
Java :: java get size of array 
Java :: how to use string variables with an if statement in java 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =