<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<script type="text/javascript">
var a, b;

a = "123";
console.log(a === 123); // false 

b = parseInt(a); // 문자열을 숫자로 변환
console.log(b); // 123
console.log(b === 123); // true

a = "1,234";
b = parseInt(a); // 1
console.log(a + " : " + b); // 1,234 : 1

a = "123a";
b = parseInt(a); // 123
console.log(a + " : " + b); // 123a : 123

a = "a123";
b = parseInt(a); // NaN
console.log(a + " : " + b); // a123 : NaN

a = "123.567";
b = parseInt(a); // 123
console.log(a + " : " + b); // 123.567 : 123

a = "";
b = parseInt(a); // NaN
console.log(a + " : " + b); //  : NaN

a = "     123";
b = parseInt(a); // 123
console.log(a + " : " + b); //      123 : 123

a = "1 2 3";
b = parseInt(a); // 1
console.log(a + " : " + b); // 1 2 3 : 1

a = "123.567";
b = parseFloat(a); // 123.567
console.log(a + " : " + b); //123.567 : 123.567


</script>
</head>
<body>

<h3>숫자로 변환 - parseInt, parseFloat</h3>

</body>
</html>

+ Recent posts