[Java/자바] printf()

sbj·2023년 11월 25일

Java

목록 보기
2/15
post-thumbnail

Printf()

It’s used to print formatted strings using various format specifiers.

‘Format specifiers’를 사용해 formatted된 String을 출력하는 데 사용된다.

  • printf() method is not only there in C, but also in Java. printf() 메소드는 C에만 있는 것이 아니라, Java에도 있다.
  • This method belongs to the PrintStream class. 이 메소드는 PrintStream 클래스에 속한다.

Syntax

System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);

Difference between String.format() and System.out.printf()

System.out.printf()과 String.format()의 차이?

  • String.format()은 format된 String을 반환한다 ↔  System.out.printf() 또한 format된 String을 반환한다.

Format Specifiers

Let’s look at the available format specifiers available for printf:

  • %c character
  • %d decimal (integer) number (base 10)
  • %e exponential floating-point number
  • %f floating-point number
  • %i integer (base 10)
  • %o octal number (base 8)
  • %s String
  • %u unsigned decimal (integer) number
  • %x number in hexadecimal (base 16)
  • %t formats date/time
  • %% print a percent sign
  • \% print a percent sign
  • %n or \n are used as line separators in printf()

Conversion Characters

The conversion-character is required and determines how the argument is formatted.

s formants strings.
d formats decimal integers.
f formats floating-point numbers.
t formats date/time values.


Line Separator

To break the string into separate lines, we have a %n specitife.


Boolean Formatting

To format Boolean values, we use the %b format.

public class Main2 {

	public static void main(String[] args) {

		System.out.printf("%b%n", null);
		System.out.printf("%B%n", false);
		System.out.printf("%b%n", 5.3);
		System.out.printf("%B%n", "random text");
	}
}

Example

public class Main {

	public static void main(String[] args) {

		System.out.printf("%d This is a format string",123 );
		
		boolean myBoolean = true;
		char myChar = '@';
		String myString =  "Bro";
		int myInt = 50;
		double myDouble = 1000;
		
		System.out.printf("%b", myBoolean);
		System.out.printf("%c ", myChar);
		System.out.printf("%s", myString);
		System.out.printf("%d", myInt);
	}
}

Example

package printf;

public class Main {

	public static void main(String[] args) {

 		System.out.printf("%d This is a format string",123 );
		
		boolean myBoolean = true;
		char myChar = '@';
		String myString =  "Bro";
		int myInt = 50;
		double myDouble = 1000;
		
 		[conversion-character]
 		System.out.printf("%b", myBoolean);
 		System.out.printf("%c ", myChar);
 		System.out.printf("%s", myString);
 		System.out.printf("%d", myInt);

 		[width] 
 		minimum number of characters to be written as output
 		System.out.printf("Hello %-10s", myString);

 		[precision]
 		sets number of digits of precision when outputting floating-point values
 		System.out.printf("You have this much money %.1f", myDouble );
		
 		System.out.printf("You have this much money %f", myDouble );
		
 		[flags]
 		adds an effect to output based on the flag added to format specifier
 		- : left-justify, +:output a plus(+) or minus (-) sign for a numeric values
 		0 : numeric values are zero-paded
 		, : comma grouping separator if numbers > 1000
		System.out.printf("You have this much money %,f",  myDouble );
	}
}

profile
Strong men believe in cause and effect.

0개의 댓글