카테고리 없음
TIL : Math 관련함수
해말그미
2024. 1. 26. 17:42
function solution(price) {
if(price >= 500000){
return Math.floor(price - (price * 0.2))
} if(price >= 300000){
return Math.floor(price -(price * 0.1))
}if(price >= 100000){
return Math.floor(price -(price * 0.05))
} else{return price}
}
주의하지 못한 점
1. 할인된 금액을 return하는 것이 아니라 "지불해야 하는 금액"이라는 것.'
2. 소수점 이하를 버린 정수를 return한다는 것.
Math.floor(price * 0.2)
처음에는 이런식으로 할인된 금액을 적어서 틀렸다.
그리고 제한사항을 제대로 보지않아 Math.floor을 쓰지 않았다.
복습
Math.floor() 함수는 주어진 숫자와 같거나 작은 정수 중에서 가장 큰 수를 반환합니다
(즉, 소수점 뒤에를 버리는데 그 숫자보다 작은 값중 가장 큰 정수를 반환 / 소수점 이하를 버림)
console.log(Math.floor(5.95)); // Expected output: 5 console.log(Math.floor(5.05)); // Expected output: 5 console.log(Math.floor(5)); // Expected output: 5 console.log(Math.floor(-5.05)); // Expected output: -6 |
Math.ceil() 은 소수점 이하를 올림
Math.ceil(0.95); // 1 Math.ceil(4); // 4 Math.ceil(7.004); // 8 Math.ceil(-0.95); // -0 Math.ceil(-4); // -4 Math.ceil(-7.004); // -7 |
Math.round()은 소수점 이하를 반올림한다.
console.log(Math.round(0.9)); // Expected output: 1 console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05)); // Expected output: 6 6 5 console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95)); // Expected output: -5 -5 -6 |
이 3개 먼가 조금씩 헷갈린단 말이지...; ㅠㅠ