Skip to content

Latest commit

 

History

History
181 lines (141 loc) · 9.01 KB

File metadata and controls

181 lines (141 loc) · 9.01 KB

#형변환 함수

  • 형변환(cast) 함수
    • parseInt(String)
    • parseFloat(String)
    • String(number)
    • Number(String)
    • Boolean(String)

###parseInt(String) parseInt(String) 함수는 문자열을 정수로 바꾸어 자바스크립트 내장 함수입니다.
이 함수는 두번째 파라미터를 확인하여 8진수, 10진수, 16진수로 변경 가능합니다.
아래는 그 예를 보여줍니다.
자바스크립트에 그대로 붙여 넣고 실행시켜 보면 주석과 같은 결과를 보실 수 있습니다.

###예시

<script>
    document.write(parseInt("20ABC")+"<BR>");              // 20 : 뒤에 문자열(ABC) 무시
    document.write(parseInt("ABC20")+"<BR>");              // NaN : 앞에 문자열(ABC) 때문에 오류
    document.write(parseInt("FF",16)+"<BR>");                // 255 : FF를 16진수로 해석
    document.write(parseInt("20",8)+"<BR>");                  // 16 : 20을 8진수로 해석
    document.write(parseInt("20",10)+"<BR>");                 // 20 : 20을 10진수로 해석
    document.write(parseInt("20",16)+"<BR>");                 // 32 : 20을 16진수로 해석
    document.write(parseInt("20.8",10)+"<BR>");              // 20 : 소숫점 이하는 버림
</script>

==> CondePen 확인

###parseFloat(String) parseFloat(String) 함수는 문자열을 부동소수점으로 바꾸어 반환하는 자바스크립트 내장 함수입니다.

###예시

<script>
    document.write(parseFloat("20.2") +"<BR>");             // 20.2
    document.write(parseFloat("20.82.7") +"<BR>");         // 20.82 : 첫번째 소숫점까지만 적용
</script>

==> CondePen 확인

parseInt()나 parseFloat()는 형변환 자체가 목적은 아니기 때문에 얘기가 나온 김에 좀 더 살펴보겠습니다.
parseIn()와 parseFloat()는 정수와 실수로 파싱해 주는 역할을 하고 있습니다.

parseInt(string, radix) parseFloat(string)

API정의를 보면 위처럼 정의되어 있습니다. parseInt()에서 radix는 기수로 parse할 때 기준이 되는 진수입니다. 정수로 파싱하는 parseInt에만 정의되어 있습니다. 2~ 36진수까지를 정의할 수 있고 optional값으로 없을 경우 10진수로 parse합니다.

parseInt("123.456");        // 123
parseInt("100mile");        // 100
parseInt("w25");               // NaN
parseInt("05");                  // 5
parseInt("09");                  // 0
parseInt("0x35");              // 53
parseInt("1101", 2);         // 13
parseInt("09", 10);            // 9
parseInt("10", 8);              // 8

parseFloat("123.456");       // 123.456
parseFloat("100.5mile");    // 100.5
parseFloat("w25");               // NaN
parseFloat("05");                  // 5
parseFloat("09");                  // 9
parseFloat("0x35");              // 0

Javascript에서 "0"으로 시작하는 숫자는 8진수 "0x"로 시작하는 숫자는 16진수로 정의되고 있기 때문에 5번라인에서 9가 8진수에서 사용할 수 없기 때문에 의도하지 않은 0이 나왔습니다.

###String(number) String(number) 함수는 숫자를 문자로 바꾸어 반환하는 자바스크립트 내장 함수이다.

###예시

<script>
    document.write(String("ABC20") +"<BR>");               // ABC20 : 화면에 보이는 대로 출력
    document.write(String("20.82.7ABC") +"<BR>");         // 20.82.7ABC : 화면에 보이는 대로 출력
</script>

==> CondePen 확인

###Number(String) Number(String) 함수는 문자를 숫자로 바꾸어 반환하는 자바스크립트 내장 함수이다.

###예시

<script>
    document.write("Number('33') + Number('55') ===> " + (Number('33') + Number('55')) + "<br><br>");
    //숫자로 변환되어 결과 값으로 33+55=88을 반환
</script>

==> CondePen 확인

// 숫자를 스트링로 바꾸기
var tt = 2;
alert(typeof tt);    // Result : number
tt = String(tt);
alert(typeof tt);    // Result : string

// 스트링을 숫자로 바꾸기
tt = "2";
alert(typeof tt);    // Result : string
tt = Number(tt);
alert(typeof tt);    // Result : number

타입변환을 하는 함수인 Number()와 String()을 이용한 소스입니다.

==> CondePen 확인

또한 아래처럼 parseInt()나 parseFloat()를 이용해서도 형변환을 할 수 있습니다. 함수명에서 알 수 있듯이 parseInt()는 Integer타입으로 변환을 하고 parseFloat()는 Float타입으로 변환을 합니다.

parseInt(String) & parseFloat(String)

var tt = "2";
alert(typeof tt);    // Result : string
tt = parseInt(tt);
alert(typeof tt);    // Result : number

tt = "2";
alert(typeof tt);    // Result : string
tt = parseFloat(tt);
alert(typeof tt);    // Result : number

==> CondePen 확인

###Boolean(String) Boolean(String) 함수는 불리언 자료형으로 변환할 때 사용하는 함수입니다.
아래 5가지를 제외한 모든 경우 true 값을 반환합니다.
문자열 '0' 과 문자열 'false'는 문자열이므로 true 값을 반환합니다.

###예시

<script>
	document.write(Boolean(0) +"<BR>"); // false
	document.write(Boolean(NaN) +"<BR>"); // false
	document.write(Boolean('') +"<BR>"); // false
	document.write(Boolean(null) +"<BR>"); // false
	document.write(Boolean(undefined)); // false
</script>

==> CondePen 확인


정리문서 바로가기


개인별문서 바로가기