A-HA💡/JS

[JS/알고리즘] 나눗셈 몫 구할 때 parseInt와 Math.floor의 차이점

탱 'ㅅ' 2023. 11. 28. 11:37

- 결론 - 

양수일 때는 동일한 결과.

음수일 때, 값의 소수점만 버리고 싶다면 parseInt(string, radix)

소수점 아래에서 -1 내림한 값을 원한다면 Math.floor(number) 

parseInt('12')            // 12
Math.floor('12')         // 12

parseInt('12 34 56')     // 12
Math.floor('12 34 56')  // NaN

 

 

 

ref.

 

parseInt() - JavaScript | MDN

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

developer.mozilla.org

 

Math.floor() - JavaScript | MDN

The Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.

developer.mozilla.org