본문 바로가기
Java/topic

printf() 지시자 정리

by Ellery 2022. 10. 30.
  • 지시자 정리
    • %b: boolean
    • %d: decimal
    • %o: octal
    • %x. %X: hexa-decimal
    • %f: floating-pint
    • %e, %E: exponential expression
    • %c: character
    • %s: String
    • %n: new line
    • 지시자 앞에 #를 넣으면 접두사 0x, 0 등 radix 정보까지 붙는다
    • %X는 16진수에 사용되는 접두사와 영문자를 대문자로 출력한다
    • char타입을 %d로 출력할려면 형변환해야된다. (C는 안해도 출력가능)
    • %.n 문자열 n글자 일부만 출력
    System.out.printf("%5d%n", 10); // 5칸만큼 오른쪽정렬
    System.out.printf("-5d%n", 10); // 왼쪽정렬
    System.out.printf("%05d%n", 10); // 나머지 공간을 0으로 채움
    System.out.printlf("[%.8s]%n", url); // 왼쪽에서 8글자만 출력
    [   10]
    [10   ]
    [00010]
    System.out.printf("%x%n", hex); // fffffffffffffffffffff
    System.out.printf("%#x^n", hex); // 0xfffffffffffffffffffff