JavaScript에서 소수점 관련 작업을 수행할 때 유용한 Math 객체의 내장 함수🤔
console.log(Math.ceil(4.2)); // 5
console.log(Math.ceil(-3.1)); // -3
console.log(Math.floor(4.9)); // 4
console.log(Math.floor(-3.1)); // -4
console.log(Math.round(4.5)); // 5
console.log(Math.round(4.4)); // 4
console.log(Math.round(-4.5)); // -4 (음수의 경우 반올림에서 0.5는 올림)
console.log(Math.round(-4.6)); // -5
console.log(Math.trunc(4.9)); // 4
console.log(Math.trunc(-4.9)); // -4
console.log(Math.fround(4.5)); // 4.5
console.log(Math.fround(1.337)); // 1.3370000123977661
console.log(Math.fround(1.5)); // 1.5
Number 객체의 메소드
const num = 4.56789;
console.log(num.toFixed(2)); // "4.57"
console.log(num.toFixed(0)); // "5"
const num = 4.56789;
console.log(num.toPrecision(2)); // "4.6"
console.log(num.toPrecision(4)); // "4.568"
프로그래머스에서 자주 나오는 소수점들... 이참에 정리해봤다✍️