How to use printf and format methods in Java

2 Answers

0 votes
package javaapplication1;

public class Example {
    public static void main(String[] args) {
        float f = 3.14f;
        int i = 985942;
        String s = "Java Programming";
	    System.out.printf("f: %.2f, i: %d, string: %s\n", f, i, s);
        System.out.format("f: %.2f, i: %d, string: %s\n", f, i, s);
    }
}
 
 
/*
run:
 
f: 3.14, i: 985942, string: Java Programming
f: 3.14, i: 985942, string: Java Programming
 
*/

 



answered Jan 12, 2016 by avibootz
0 votes
package javaapplication1;

public class Example {
    public static void main(String[] args) {
	    System.out.printf("f: %.2f, i: %d, string: %s\n", 2.15f, 83736, "Java");
        System.out.format("f: %.2f, i: %d, string: %s\n", 2.15f, 83736, "Java");
    }
}
 
 
/*
run:
 
f: 2.15, i: 83736, string: Java
f: 2.15, i: 83736, string: Java
 
*/

 



answered Jan 12, 2016 by avibootz

Related questions

1 answer 202 views
1 answer 170 views
170 views asked Jun 12, 2019 by avibootz
1 answer 152 views
1 answer 127 views
127 views asked Jun 30, 2020 by avibootz
1 answer 106 views
106 views asked Jul 24, 2022 by avibootz
...