JavaScript
[#. JavaScript] 소수점 올림, 소수점 내림, 소수점 반올림, 소수점 반올림 후 숫자를 문자열로 변환
shab
2020. 11. 18. 17:12
반응형
소수점 올림, 내림, 반올림, 반올림 해서 문자열로 출력 하는 방법을 알아보자
소수점 올림
Math.ceil(89.123); // 90
Math.ceil(89.678); // 90
소수점 버림
Math.floor(89.123); // 89
Math.floor(89.678); // 89
소수점 반올림
Math.round(89.123); // 89
Math.round(89.678); // 90
toFixed
지정한 소수점 이하 숫자를 반올림한 후 숫자 -> 문자열 변환하여 출력
toFixed(2) => 소수점 3번째 자리에서 반올림
(89.123).toFixed(0) // "89"
(89.123).toFixed(2) // "89.12"
(89.123).toFixed(4) // "89.1230"
(89.678).toFixed(0) // "90"
(89.678).toFixed(2) // "89.68"
(89.678).toFixed(4) // "89.6780"
반응형