파이썬의 //
가 JS에서는 존재하지 않는다! 다음 두가지 방법을 통해 구해보자
Math.floor
var q = Math.floor(13 / 5)
console.log(q) // 2
parseInt
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/parseInt
parseInt()
함수는 문자열 인자를 파싱하여 특정 진수(수의 진법 체계에서 기준이 되는 값)의 정수를 반환
parseInt(string, radix)
;
string
radix
(optional)주의
parseInt
를 Math.floor()
의 대체품으로 사용해서는 안된다!
6.022e23
(6.022 × 10^23)처럼 문자열 표현에 e
문자를 사용하기 때문에, parseInt
를 매우 크거나매우 작은 숫자의 소수점 이하 값을 자르기 위해 사용하면 예기치 못한 결과가 발생할 수 있습니다.console.log(parseInt('123'));
// 123 (default base-10)
console.log(parseInt('123', 10));
// 123 (explicitly specify base-10)
console.log(parseInt(' 123 '));
// 123 (whitespace is ignored)
console.log(parseInt('077'));
// 77 (leading zeros are ignored)
console.log(parseInt('1.9'));
// 1 (decimal part is truncated)
console.log(parseInt('ff', 16));
// 255 (lower-case hexadecimal)
console.log(parseInt('0xFF', 16));
// 255 (upper-case hexadecimal with "0x" prefix)
console.log(parseInt('xyz'));
// NaN (input can't be converted to an integer)
console.log(parseInt("546", 2);
// NaN (0과 1을 제외한 숫자는 2진법에서 유효하지 않음)