[Java] toBinaryString(), String.format()

한호성·2022년 7월 20일
0

Integer.toBinaryString()

static String toBinaryString(int i)
Integer 형 정수를 이진법으로 바꾼 문자열로 바꾸는 함수이다.

ex) 8 -> "1000"
*cf) 16진수 toHexString(), 8진수 toOctalString() 도 존재한다.

Integer.parseInt()

static int parseInt(String s , int radix)
String 문자열은 int 형으로 변환하는 함수이다.

radix 값에 String이 몇진수인지 나타낼 수 있다.
ex) Integer.parseInt("1000",2) -> 8
ex) Integer.parseInt("10F",16") -> 271
#cf) default radix값은 10;

String.format()

public static String format(String format, Object args);
문자열 형식을 지정할 수 있는 함수이다.

parameter 인 format 에는 문자열의 형식을 정한다.
1. %d (10진수 형식)

  • %5d : 숫자가 사이에 들어가면 글자 길이를 설정할 수 있고,주어진 object가 숫자의 길이보다 작다면 공백을 채운다.
  • %,d : 3자리단위로 쉼표를 찍어준다
  • %-d : 기본적으로 오른쩍 정렬이고, -를 붙일 경우 왼쪽 정렬된다.
  1. %s (문자열 형식)
  • %5s : 문자열의 길이가 5보다짧을 경우 나머지를 공백으로 채운다.
  • %.2s : 문자열의 길이를 2만큼만 사용한다.

Reference

https://onepinetwopine.tistory.com/768
https://blog.jiniworld.me/68

profile
개발자 지망생입니다.

0개의 댓글