printf method in java:
#1. Write a program to print string using print() and printf() methods
Output:
java printf table
Format double using printf():
#2. Write a program to print double to 2 decimal places in java
Output:
#3. Write a program to format text using printf() method in java
- Print() and println() methods are used to print text and object values or format data.
- In order to format text we also have printf() method in java
- Formatting data means displaying corresponding data type value.
- For example when print float ot double value if we want to specify upto 2 decimal numbers we user.
- System.out.printf("%.2f", variable);
- Also alignment of string data. when we are printing string data in java using print() and pintf()
#1. Write a program to print string using print() and printf() methods
- package printfinjava;
- /**
- * How to format text using java printf() method
- * @author www.instanceofjava.com
- */
- public class PrintfMethod {
- public static void main(String[] args) {
- String str="java printf double";
- System.out.println ("String is "+str);
- System.out.printf ("String is %s", str);
- }
- }
- String is java printf double
- String is java printf double
java printf table
Format double using printf():
- We can format double using printf() method in java
- format double to 2 decimal places in java possible by using system.out.printf() method.
#2. Write a program to print double to 2 decimal places in java
- package printfinjava;
- /**
- * How to format text using java printf() method
- * @author www.instanceofjava.com
- */
- public class PrintfMethod {
- public static void main(String[] args) {
- double value=12.239344;
- System.out.printf("%.2f", value);
- }
- }
- 12.24
#3. Write a program to format text using printf() method in java
No comments