본문 바로가기
Develope_Web&App/01_Java & JavaFX

[java] string 변환시키기

by 스타트업_디벨로퍼 2021. 1. 27.

1. 문자열에서 정수형으로 변환 (String to int)

- int i = Integer.parseInt(String str);

 

2. 정수형을 문자열로 변환 (int to String)

- String str = Integer.toString(int i);

- String str = String.valueOf(int i);  

 

3. 형식에 맞춰서 변수들을 문자열로 변환 (c언어의 printf 동일)

- int i; float f;

  String str = String.format("%d %f",i,f);

 

4. 문자열에서 다른 숫자형태로 변환

- float f = Float.parseFloat(String str);  // String to float

- double d = Double.parseDouble(String str); // String to double

- byte b = Byte.parseByte(String str); // String to byte

- long l = Long.parseLong(String str); // String to long

- short s = Short.parseShort(String str); // String to short

 

5. 다른 숫자형태에서 문자열로 변환

- String str = String.valueOf(boolean b); // true 또는 false가 str에 저장됩니다.

- String str = String.valueOf(char c); // char to String

- String str = String.valueOf(char[] data); // char array to String

- String str = String.valueOf(double d); // double to String

- String str = String.valueOf(float f); // float to String

- String str = String.valueOf(long l); // long to String

- String str = String.valueOf(Object o); // Object to String , o == null이면 "null" 이 되고 o != null이면 o.toString() 함수의 반환값이 str이 된다.

- String str = String.valueOf(char[] data, int offset, int count); // offset 의 index부터 count 개의 문자로 부분문자열 생성



출처: https://babytiger.tistory.com/entry/JAVA-문자열String과-다른-변수로short-int-long-float-double-byte의-변환 [성장하는 호랑이]

 

println()의 단점 - 출력 형식 지정 불가 

- 실수의 자리수 조절 불가 

ex) System.out.println(10.0/3);           // 3.333333

- 10진수로만 출력 

System.out.println(0x1A);                //26 

 

printf()

- 출력 형식 지정 가능 

System.out.printf("%.2f", 10.0/3);        // 3.33 소수점 둘째 자리 까지 출력

System.out.printf("%d", 0x1A);           // 26  10진수 

 

System.out.printf("%X", 0x1A);           //1A   16진수 

( printf()는  println() 처럼 자동 줄바꿈 X ) 

 

printf() 지시자 

지시자 설명
%b 불리언(boolean) 형식으로 출력
%d 10진(decimal) 정수의 형식으로 출력
%o 8진(octal) 정수의 형식으로 출력
%x, %X 16진(hexa_decimal) 정수의 형식으로 출력
%f 부동 소수점의 형식으로 출력
%e, %E 지수 표현식의 형식으로 출력
%c 문자로 출력
%s 문자열로 출력 

 

예제 

- 정수를 10진수, 8진수, 16진수, 2진수로 출력 

System.out.printf("%d", 15) ;                                //15          10진수

System.out.printf("%o", 15);                                 //17           8진수

 

System.out.printf("%x", 15);                                 // f            16진수 

System.out.printf("%s", Integer.toBinaryString(15));   // 1111           2진수 s는 문자열 출력 

 

- 8진수와 16진수에 접두사 붙이기 

System.out.printf("%#o", 15) ;                             // 017 

System.out.printf("%#x", 15);                              // 0xf

System.out.printf("%#X", 15);                              // 0XF

 

- 실수 출력을 위한 지시자 %f 

- 지수형식 %e  (간략한 형식 %g) 

float f = 123.4567890f; 

System.out.printf("%f", f);                                   // 123.456787     소수점아래 6자리 

System.out.printf("%e",f);                                   //  1.23458e+02   지수 형식 

*float는 정밀도가 7이여서 7자리까지 정확 뒤의 숫자는 의미없는 숫자 

 double은 정밀도가 15 따라서 15자리까지 정확! 

double f = 123.456789;

System.out.printf("%f%n", f);                            //123.456789

System.out.printf("%e%n", f);                           //1.234568e+02

 

System.out.printf("%g,%n",f);                           //123.457

 

System.out.printf("[5d]%n", 10);              //[   10]         5자리 출력

System.out.printf("[5d]%n", 123456);        // [123456]     5자리 출력이지만, 입력이 123456이라서 [123456] 출력

System.out.printf("[%-5d]%n", 10);           // [10   ]       -는 왼쪽정렬

System.out.printf("[%05d]%n',  10);          //[00010]        빈자리는 0을 붙임 

 

%전체자리.소수점아래자지f 

System.out.printf("%14.10f%n", d);          // 전체 14자리 중 소수점 아래 10자리 

소수점 앞자리의 경우 빈자리는 공백, 소수점 뒷자리의 경우 빈자리는 0 

 

문자열의 경우 지시자 s 사용 

System.out.printf("[%s]%n", url) ;                   //[www.codechobo.com]

System.out.printf("[%20s]%n", url);                 //[공백www.codechobo.com]

System.out.rpintf("[%-20s]%n", url);                //[www.codechobo.com공백] 

System.out.printf("[.8s]%n", url);                    // 문자열의 일부만 출력 가능 [www.code] 전체 문자열 중 8글자만 출력 

 

반응형

'Develope_Web&App > 01_Java & JavaFX' 카테고리의 다른 글

[Java의 정석] 객체지향 1-1  (0) 2021.02.03
[Java의 정석] 객체 지향 1  (0) 2021.02.03
[java] 예외처리  (0) 2021.01.27
[Java] Chap04 조건문과 반복문  (0) 2021.01.27
[Java] 03 연산자(Operator)  (0) 2021.01.26